difference between pointer and reference | | |
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?
More specifically, is there anything really different between these
two “queue”s? | | | | re: difference between pointer and reference
On 2008-07-13 10:56, thomas wrote: Quote:
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.
--
Erik Wikström | | | | re: difference between pointer and reference
On Jul 13, 5:44*pm, Erik Wikström <Erik-wikst...@telia.comwrote: Quote:
On 2008-07-13 10:56, thomas wrote:
> Quote:
I’m just writing a program which uses the queue stl type.
> Quote:
Queue<packet**queue_;
> Quote:
Queue<packet&*queue_;
> Quote:
These two writings are very similar, except that one is pointer and
one is reference.
> Quote:
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.
>
--
Erik Wikström
why not? References just mean another name for these packet object.
It's correct in my understanding.
Any more details to clarify this? | | | | re: difference between pointer and reference
On 2008-07-13 11:54, thomas wrote: Quote:
On Jul 13, 5:44 pm, Erik Wikström <Erik-wikst...@telia.comwrote: Quote:
>On 2008-07-13 10:56, thomas wrote:
>> Quote:
I’m just writing a program which uses the queue stl type.
>> Quote:
Queue<packet* queue_;
>> Quote:
Queue<packet& queue_;
>> Quote:
These two writings are very similar, except that one is pointer and
one is reference.
>> Quote:
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.
Please do not quota signatures. Quote:
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.
--
Erik Wikström | | | | re: difference between pointer and reference
"Erik Wikström" <Erik-wikstrom@telia.comskrev i en meddelelse
news:x2mek.957$U5.690@newsb.telia.net... Quote:
On 2008-07-13 11:54, thomas wrote: Quote:
>On Jul 13, 5:44 pm, Erik Wikström <Erik-wikst...@telia.comwrote: Quote:
>>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.
>
Please do not quota signatures.
> Quote:
>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? | | | | re: difference between pointer and reference
On 2008-07-13 18:48, rufus wrote: Quote:
"Erik Wikstré—£" <Erik-wikstrom@telia.comskrev i en meddelelse
news:x2mek.957$U5.690@newsb.telia.net... Quote:
>On 2008-07-13 11:54, thomas wrote: Quote:
>>On Jul 13, 5:44 pm, Erik Wikstré—£ <Erik-wikst...@telia.comwrote:
>>>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.
>>
>Please do not quota signatures.
>> Quote:
>>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.
--
Erik Wikström | | | | re: difference between pointer and reference
On Jul 13, 11:44 am, Erik Wikström <Erik-wikst...@telia.comwrote: Quote:
On 2008-07-13 10:56, thomas wrote:
Quote: Quote:
I’m just writing a program which uses the queue stl type.
Quote: Quote:
Queue<packet* queue_;
Quote: Quote:
Queue<packet& queue_;
Quote: Quote:
These two writings are very similar, except that one is
pointer and one is reference.
Quote: Quote:
Is there anything I must be careful when I am using either
one of them?
Quote:
Yes, only one of them is legal,
Actually, neither of them are legal. There isn't any class
Queue in the standard. Of course, if he really means
std::queue, then your comments are correct. (But of course, if
he really means std::queue, that's what he should write.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
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 | | | | re: difference between pointer and reference
On Jul 13, 7:27 pm, Erik Wikström <Erik-wikst...@telia.comwrote: Quote:
On 2008-07-13 18:48, rufus wrote: Quote:
"Erik Wikström" <Erik-wikst...@telia.comskrev i en meddelelse
news:x2mek.957$U5.690@newsb.telia.net... Quote:
On 2008-07-13 11:54, thomas wrote:
>On Jul 13, 5:44 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
>>On 2008-07-13 10:56, thomas wrote:
Quote: Quote: Quote:
>I'm just writing a program which uses the queue stl type.
Quote: Quote: Quote:
>Queue<packet* queue_;
Quote: Quote: Quote:
>Queue<packet& queue_;
Quote: Quote: Quote:
>These two writings are very similar, except that one is
>pointer and one is reference.
Quote: Quote: Quote:
>Is there anything I must be careful when I am using
>either one of them?
Quote: Quote: Quote:
>>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.
Quote: Quote: Quote:
>why not? References just mean another name for these
>packet object.
Quote: Quote: Quote:
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.
Quote: Quote: Quote:
* At least not according to the C++ standard.
Quote: Quote:
Is a reference not basically just a const pointer?
Quote:
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ée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,537 network members.
|