Re: difference between pointer and reference
On Jul 13, 7:27 pm, Erik Wikstr=F6m <Erik-wikst...@telia.com> wrote:
On 2008-07-13 18:48, rufus wrote:
"Erik Wikstr=F6m" <Erik-wikst...@telia.com> skrev i en meddelelse
news:x2mek.957$U5.690@newsb.telia.net...
On 2008-07-13 11:54, thomas wrote:
On Jul 13, 5:44 pm, Erik Wikstr=F6m <Erik-wikst...@telia.com> wrote:
On 2008-07-13 10:56, thomas wrote:
I'm just writing a program which uses the queue stl type.
Queue<packet*> queue_;
Queue<packet&> queue_;
These two writings are very similar, except that one is
pointer and one is reference.
Is there anything I must be careful when I am using
either one of them?
Yes, only one of them is legal, you can not have a queue
of references, you can either have a queue of pointers to
packets or you can have a queue of packets, but not a
queue of references to packets.
why not? References just mean another name for these
packet object.
Since references are just another name for the object it is
not an object in its own right (it does not occupy any
memory and does not have an address*), which means you can
not store it in a container.
* At least not according to the C++ standard.
Is a reference not basically just a const pointer?
That might be how the compiler vendors implements it (at least
in some cases) but there are a number of semantic differences
between references and const pointers. As an example a pointer
can be a null-pointer while a reference always refers to a
valid object, a const reference can bind to a temporary, a
pointer can not.
The critical point, of course, is that references are not
objects, and a standard container can only contain objects. Or,
the fact that references do not meet the requirements Assignable
and CopyConstructable. Take your pick.
--
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