Given my variable being a pointer, if I assign it to a variable of "auto" type, do I specify the "*" ?
std::vector<MyClass> *getVector(); //returns populated vector //... std::vector<MyClass> *myvector = getVector(); //assume has n items in it auto newvar1 = myvector; // vs: auto *newvar2 = myvector; //goal is to behave like this assignment: std::vector<MyClass> *newvar3 = getVector();
I'm a bit confused on how this auto
works in c++11 (this is a new feature to c++11, right?)
Update: I revised the above to better clarify how my vector is really populated in a function, and I'm just trying to assign the returned pointer to a variable. Sorry for the confusion
auto_ptr is a class template that was available in previous versions of the C++ standard library (declared in the <memory> header file), which provides some basic RAII features for C++ raw pointers. It has been replaced by the unique_ptr class.
Pointer assignment between two pointers makes them point to the same pointee. So the assignment y = x; makes y point to the same pointee as x . Pointer assignment does not touch the pointees. It just changes one pointer to have the same reference as another pointer.
Why do we need to specify the type of the data whose address, a pointer will hold, if all pointers are the same. Since all pointers store addresses. Also, the amount of space a pointer will require in memory depends on whether the machine is 32-bit or 64-bit.
When pointer assignment occurs, any previous association between the pointer object and a target is terminated. Pointers can also be assigned for a pointer structure component by execution of a derived-type intrinsic assignment statement or a defined assignment statement.
auto newvar1 = myvector; // vs: auto *newvar2 = myvector;
Both of these are the same and will declare a pointer to std::vector<MyClass>
(pointing to random location, since . So basically you can use any one of them. I would prefer myvector
is uninitialized in your example and likely contains garbage)auto var = getVector()
, but you may go for auto* var = getVector()
if you think it stresses the intent (that var
is a pointer) better.
I must say I never dreamt of similar uncertainity using auto
. I thought people would just use auto
and not think about it, which is correct 99 % of the time - the need to decorate auto
with something only comes with references and cv-qualifiers.
However, there is slight difference between the two when modifies slightly:
auto newvar1 = myvector, newvar2 = something;
In this case, newvar2
will be a pointer (and something must be too).
auto *newvar1 = myvector, newvar2 = something;
Here, newvar2
is the pointee type, eg. std::vector<MyClass>
, and the initializer must be adequate.
In general, if the initializer is not a braced initializer list, the compiler processes auto
like this:
It produces an artificial function template declaration with one argument of the exact form of the declarator, with auto
replaced by the template parameter. So for auto* x = ...
, it uses
template <class T> void foo(T*);
It tries to resolve the call foo(initializer)
, and looks what gets deduced for T
. This gets substituted back in place of auto
.
If there are more declarators in a single declarations, this is done for all of them. The deduced T
must be the same for all of them...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With