C++/difference between pointers and mem location
I still dont understand what is the difference between pointers(*var) and memory locations (&var)
what should each return??
also, what is the difference
between calling a function as objectName->function() and
1. int i;
2. int *i;
3. int &i;
1. i defines a location in memory that holds an integer. The compiler reads/writes the integer using 'i';
2. *i defines a location in memory holding an address. If the location is an address of an integer (i = &x), the compiler accesses the address using 'i' and it accesses the integer using *i. A pointer is an address and * tells the compiler to load what is at the address (as opposed to the address itself)
3. &i is a pointer (called a reference) and it, in essence saves from having to use the * operator. A reference to a struct allows you to use the . operator instead of the -> operator for a pointer to struct.
Returning a reference is unusual in code other than operators. The reason it's used in operators is because you want the object returned to be the final result. Without the reference the operator returns a temporary which has to be created and copied.
There really isn't a difference in the 2 return statements - one would have a reference passed in (you wouldn't return a reference to a stack-based object) and the other would have an address.
objectName->function() means objectName is a pointer to the class.
objectName.function() means objectName is an instantiation, not allocated.
class someClass x, *px;
These are the same:
and these are the same:
Hope this helps some.