views:

140

answers:

4

I'm just learning c++, and coming from c, some function calls I'm seeing in the book confuse me:

char a;
cin.get(a);

In C, this couldn't possibly work, if you did that, there would be no way to get the output, because you are passing by value, and not by reference, why does this work in c++? Is referencing and dereferncing made implicit (the compiler knows that cin.get needs a pointer, so it is referenced)?

+4  A: 

Reference types work like that (pass by reference, instead of passing by value). It is similar to passing a pointer (pass by pointer) in C, but with a nicer syntax and more safety (compile-time checks). The implementation passes by pointer internally.

Tronic
Ninefingers
It's not required that references be implemented as pointers if I'm not mistaken, although they typically are.
Eric
Not required, but AFAIK all implementations do it like that (for function calls, anyway).
Tronic
+1  A: 

C++ allows pass by reference (note the &, which signifies that a reference is used):

void foo(char& bar) {
    bar = 42;
}

int main() {
    char a;
    foo(a);
    // a == 42 here
}
Chris Jester-Young
+11  A: 

C++

This would work in C++ because the function signature for get() is probably this:

void get(char& a); // pass-by-reference

The & symbol after char denotes to the compiler than when you pass in a char value, it should pass in a reference to the char rather than making a copy of it.

What this essentially means is that any changes made within get() will be reflected in the value of a outside the method.

If the function signature for get() was this:

void get(char a); // pass-by-value

then a would be passed by value, which means a copy of a is made before being passed into the method as a parameter. Any changes to a would then only be local the method, and lost when the method returns.

C

The reason why this wouldn't work in C is because C only has pass-by-value. The only way to emulate pass-by-reference behaviour in C is to pass its pointer by value, and then de-reference the pointer value (thus accessing the same memory location) when modifying the value within the method:

void get(char* a)
{
    *a = 'g';
}

int main(int argc, char* argv[])
{
    char a = 'f';
    get(&a);
    printf("%c\n", a);

    return 0;
}

Running this program will output:

g
LeopardSkinPillBoxHat
+1  A: 

This is why some C++ programmers don't like to use references like that.
It looks like a function arguement - you have no clue from the syntax whether 'a' is modified or not.

Prefer passing const references (when you have a large object and a copy would be expensive) and returning references or best of all passing pointers.

Martin Beckett
-1 because returning by parameter is perfectly valid and good style (and experienced C++ programmers, including the standards committee, do it).
Tronic
@Martin - this doesn't work too well when a method needs to return multiple values.
LeopardSkinPillBoxHat
Returning references can be asking for trouble, since the caller can't control the lifetime of the object that the returned reference is referencing. In particular, if you return a reference to a stack object, your caller will be in for a lot of grief :)
Jeremy Friesner
I have seen that said before: 'experienced c++ programmers don't like to use references'... and still wonder who those 'experienced c++ programmers' are. I find that statement a little subjective, unless you can provide with literature to that respect. I have not seen that statement in any of the books i have read by well known C++ gurus.
David Rodríguez - dribeas