Re: What's the point of references?
Victor Bazarov wrote:
kanze wrote:
johnchx2@yahoo.com wrote:
liberal@liberalmessiah.com wrote:
Pointers can do everything that references can do, plus,
be assigned. So, what's the point of references?
Two words: operator overloading. It's impossible to express
the parameter types of a user-defined operator+(), for
example, without references.
Not really. You can pass values. Of course, if the objects
are non-trivial, you may take a performance hit.
It's not just that. Passing values causes slicing, while references
are very well suited for polymorphic use.
Also a good point in general. In the case of operators,
however, I'm not sure. In my experience, overloaded operators
and polymorphism don't go well together. You're certainly going
to return by value, so you have to be explicit about the type at
one point, at least. There are ways of dealing with this (e.g.
the letter-envelop idiom), but for the most part, they resolve
to adding an extra, hidden indirection, so that you have the
impression of dealing with a single type (and slicing doesn't
matter).
--
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! ]