tags:

views:

340

answers:

5

I've never had problems with references as in Python (implicit) or PHP (explicit &). In PHP you write $p = &$myvar; and you have $p as a reference pointing to $myVar.

So I know in C++ you can do this:

void setToSomething( int& var )
{
 var = 123;
}
int myInt;
setToSomething( myInt );
  • Myint is now 123, why?

Doesn't & mean "memory address of" x in C++? What do I do then if var is only the adress to myInt and not a pointer?

void setToSomething( int* var )
{
 *var = 123;
}
int myInt;
int* myIntPtr = &myInt;
setToSomething( myIntPtr );
  • Does the above work as expected?

I don't understand the difference between * and & in C++ fully. They tell you & is used to get the adress of a variable, but why does that help you in functions etc. like in the first example?

+6  A: 

In the first example, & is used to declare a reference type. It's not the same thing as the & operator which is used to get an object's address.

You can view a reference type as a type which uses under the covers a pointer which can never be NULL.

Bastien Léonard
So why use pointers when I can have reference types everywhere?
gnm
@gnm: You can do things, evil things, with pointers that you can't with references. Sometimes those are necessary.
Donal Fellows
@gnm in c/c++ pointers are often used in the performance vs safety tradeoffs. Especially in image manipulation, using (wisely) pointers will give you the ultimate performance
Vivek Bernard
@gnm: The advantage of pointers over references is that you can later reset them to point to some other object. That makes them more flexible. The advantage of references over pointers is that they cannot be reset to point to any other object than the one they were initialized with. Since this includes that they cannot (well, it's harder to have them) point to a no longer existing object, that makes them safer.
sbi
@Vivek: I don't see how using pointers would give you a speed advantage over references.
sbi
@sbi: I agree. Since references are almost certainly implemented as pointers by the compiler, the exact same code should be output by the compiler for both the reference andf the pointer version.
KeithB
A: 

In the second example it should be

*var = 123

Think of the ampersand in the argument declaration as the var keyword in Pascal, it's used to specify that the variable will be updated if changed. I'm not sure if the C++ semantics specify that it will be passed always by reference or it could be done via a pass by value and copy on return if it's more efficient for a given case...

fortran
A: 

& is used in functions when you need to return more than one variable value unlike the return statement which returns a single variable.

Dave18
I knew that but thanks that you reminded me :) :)
gnm
It also wasn't my question but still, great answer
gnm
+6  A: 

Doesn't & mean "memory address of" x in C++?

Short answer: That depends.

Somewhat longer answer:
The unary prefix operator &, when applied to an object, indeed yields the address of the object: &obj. There is, however, also the type modifier &, which, when applied to a type, will modify it to be a reference: int&.

The same applies to *: When used as a unary prefix operator, it will dereference a pointer: *ptr. When used as a type modifier, it will modify the type to be a pointer: int*.

It's not helpful either that the type modifiers apply to the variable that is declared. For example, this

int *p, **pp, i, &r = i; 

defines an int pointer, a pointer to a pointer to an int, a vanilla int, and an int reference. (The latter is immediately initialized, because you cannot have an uninitialized reference.) Note that the modifiers syntactically belong to the declared variable whose type they are modifying, not to the type of the declared variable. Nevertheless, in this example the * and & modify the type of the variable.
In the following case, however, with p and i presumed to be variables

*pp = &i;

* and & are unary prefix operators dereferencing pp and yielding the address of i.


To complicate things even further, besides the type modifiers and the unary prefix operators & and *, there are also the binary infix operators & and *, meaning "bitwise AND" and "multiplication". And to add insult to injury, in C++ you can overload both the unary prefix and the binary infix variants of the operator for user-defined types and be completely free as to their meaning.

sbi
A: 

Actually, the &-operator serves to purposes as posted above.

the first purpose is dereference, which is used like this:

int i = 123; //declare an integer variable named i with the value of 123
int *pI = &i; //declare an pointer-to-integer 
              //variable name pI pointing to the memory address of i

The &-operator is used in the meaning 'give me the memory address of'

the second purpose is to emulate call-by-reference (which c does not have natively)

a decleration like

void foo(int& theInt)
{
  theInt = 123;
}

will make the function foo accept an int parameter, to which any modification during the execution of foo is passed back.

Without the &-operator, modifications to theInt would be made in the foo's scope only and discarded once the function terminates.

sum1stolemyname