CarbonMike said:(it sucks that the reference declarator is the same as the address-of operator; very confusing IMO)
almightyshoe said:References are typically implemented as pointers in the underlying generated code.
ifjake said:Hey thanks guys.
So this means the compiler generates the same code for references and pointers? What's the advantage of using one over the other? I suppose there is pointer arithmetic (maybe?)..
CarbonMike said:You cannot do pointer arithmetic on a reference
Unlike a pointer, you can't dereference a reference
You can't make a reference point to anything else once you've created one
int x = 0;
int& y = x;
int* ptr = (int*) (&y);
class X { ... };
X x;
X& y = x;
X& z;
z = x;
X a;
y = a;
z = a;
CarbonMike said:Now what's the advantage of using one over the other? Again, it's all about the call semantics. If I write a function which needs to modify the thing being passed (rather than a copy of it) and I DON'T want to use pointer semantics, it's reference time.
The best example of why this is useful is operator overloading. In fact, I'd hazard a guess that references were a side effect of another language feature.
MarkCollette said:Of course you can dereference a reference and do pointer arithmetic with it, because it uses the same syntax as the base type (int in my example).
MarkCollette said:And yes you can reuse references, it's just a really bad idea.
class X { ... };
X x;
X& y = x;
X& z;
z = x;
X a;
y = a;
z = a;
MarkCollette said:Exactly. References were added to make things easier for people who just can't wrap their heads around pointer syntax.
CarbonMike said:This is just about true -- although your sample code cited below certainly will not compile, because you're creating a reference (X& z) without assigning it to anything. Reuse in that sense is verboten.
Code:class X { ... }; X x; X& y = x; X& z; z = x; X a; y = a; z = a;
But you're right, and I was wrong, concerning the ability to make a reference point to something else once it's been assigned. I also agree that it's a terrible idea!
CarbonMike said:I couldn't disagree more on this point. I think references are a part of C++ because you can't do syntactically credible operator overloading without them, given the function call semantics of the language. Hell, if it's one thing C++ *doesn't* accomodate, it's people who can't wrap their heads around tricky syntax!
//Calling a method on an object:
X x;
x.method();
// Calling a method on a reference:
X y;
X& x = y;
x.method();
// Calling a method on a pointer using pointer specific syntax:
X y;
X* x = &y;
x->method();
// Calling a method on a pointer using object-ish syntax:
X y;
X* x = &y;
(*x).method();
MyClass & MyClass::operator=(const MyClass & original);
MyClass::MyClass(const MyClass & original);
MyClass MyClass::operator=(const MyClass original);
MyClass a, b;
a = b; // this could (should?) created a temporary for "original" from "b".
MyClass MyClass::operator=(const MyClass * original);
MyClass a, b;
a = &b;
MarkCollette said:And yes you can reuse references, it's just a really bad idea.
Code:class X { ... }; X x; X& y = x; X& z; // error! z = x; // crash! X a; y = a; // copy a into x. y still refers x. z = a; // crash, again!
I think (you should check) that z will now point to a, whereas y will have invoked x's assignment operator with a as the parameter. Wierdness like that made me change a bunch of code from references to pointers, so that noob programmers couldn't accidently mess things up.
Gil Bates said:References need to be initialized immediately. So, in your example, "X& z;" should be an error. Even if your compiler doesn't give you an error, the next line "z = x" can crash because "z" refers nothing (and the pointer that implements it points nowhere).
Also, references cannot be "reused". There is no way to do it. The above "y" always referes "x" (and "z" always refers nothing if you can get it to compile ).
Hope this helps,
Gil
MarkCollette said:That example I gave for the references, is not legal, but was allowed by Visual C++ 5 and 6, Sun's C++ compiler, and DEC Alpha's C++ compiler. And none of them crashed. But yes, the point I was trying to make was that once assigned, subsequent "assignments" would simply be operator= calls on the first object.
template<class X> void Swap(X & a, X & b)
{
X c; // I didn't use "X c = a;", so it won't require copy constructors.
c = a; // assignment operators should be defined using references
a = b; // to be efficient as in my previous post.
b = c;
}
Gil Bates said:Sorry to hear your code didn't even crash. If it did, it would've been much easier to find the bug . I'm a bit surprised none of your compilers didn't give you an error. If I'm not mistaken, references were alreay in in the 1st edition of Stroustrup's book back in the late 80s.
Gil Bates said:The primary difference between pointers and references may be just syntax. But sometimes, syntax is very important. Initially, I didn't get the importance of references either. But, for example, when you write a template function or class, it's very important that class objects and built-in type objects behave in the same way.