Re: pointer vs reference
On 5 avr, 02:33, "Jim Langston" <tazmas...@rocketmail.com> wrote:
Eric Kaplan wrote:
more specific - for passing parameter, what's different
between & OR *
pointer vs reference ??
Request(string const& start, string const& end);
Request(string const* start, string const* end);
A reference is an alias. It is similar to a pointer but with
some changes.
[...]
Good posting, but you missed the two most obvious differences
from the user's point of view (and thus, to take into
consideration when designing the interface):
-- A reference cannot be null; it must designate an actual
object. If you want an optional value, you'll have to use a
pointer.
-- A pointer cannot be initialized to point to a temporary. If
you want your client to be able to pass the results of an
arbitrary expression, then you must use a reference to
const.
Because of these considerations, from a user's point of view, a
reference to a const is actually more like pass by value than a
pointer, i.e. given:
void func1( std::string ) ;
void func2( std::string const& ) ;
void func3( std::string const* ) ;
, there is almost no difference between the first two for the
user; it is the third that is different.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34