Re: What's the point of references?
Seungbeom Kim wrote:
Ralf Fassel wrote:
| Pointers can do everything that references can do, plus, be
| assigned. So, what's the point of references?
They can't be NULL, so you can skip the is-pointer-non-NULL check.
It's true that reference cannot be NULL, but please note that
it doesn't mean references are automatically, always valid.
int* p = new int(2);
int& r = *p;
int i = r; // Oops! r is an invalid reference here.
I would rather say that you cannot check the validity even if you
want to, than that you can skip the check.
In practice, they can also be null in production builds. Just
replace the new int(2) above with NULL. The real difference is
only where the code will core dump: in a debug build, it will
core when you try to create the reference; with a pointer, it
will only core when you dereference the pointer. (And of
course, a lot of compilers don't support debug builds.)
Another effective difference is, because the compiler knows it
cannot be null, it doesn't have to check, e.g. when converting
between classes in a hierarchy.
The benefit of having references is that you have another way
to express the responsibility of checking. If a function takes
a pointer, it's usually the callee that is expected to check
if the pointer is null and act accordingly. If a function
takes a reference, the callee CANNOT check whether the
argument is valid or not, so it's the caller's responsibility
to pass a valid reference and the callee assumes the validity.
Practically speaking, there is no way in C++ to test whether a
pointer is valid or not. You can only test for null. The
distinction between a reference and a pointer, when used as a
parameter or a return value, is that a pointer can be
null---it's a legal pointer value, and often serves as a signal
value---where as a reference can't. A typical convention is
that if the function takes a pointer, it will do something
intelligent with a null pointer, but if it takes a reference, it
must be given a valid object. But there are exceptions, either
for historical reasons or technical reasons. (Consider
std::string( char const* ), for example.)
Another important difference is that if the reference is const,
you can bind an rvalue to it; you can never take the address of
And finally, of course, you can't use pointers when overloading
operators. For smallish types (e.g. std::complex), passing by
value is probably acceptable, but if you're implementing matrix
addition for matrixes with a couple of million elements, I doubt
that your users would appreciate the joke.
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]