Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > difference between pointer and reference

Reply
Thread Tools

difference between pointer and reference

 
 
thomas
Guest
Posts: n/a
 
      07-13-2008
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?
 
Reply With Quote
 
 
 
 
Erik Wikström
Guest
Posts: n/a
 
      07-13-2008
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.

--
Erik Wikström
 
Reply With Quote
 
 
 
 
thomas
Guest
Posts: n/a
 
      07-13-2008
On Jul 13, 5:44*pm, Erik Wikström <(E-Mail Removed)> 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.
>
> --
> 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?
 
Reply With Quote
 
Erik Wikström
Guest
Posts: n/a
 
      07-13-2008
On 2008-07-13 11:54, thomas wrote:
> On Jul 13, 5:44 pm, Erik Wikström <(E-Mail Removed)> 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.


Please do not quota signatures.

> 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
 
Reply With Quote
 
rufus
Guest
Posts: n/a
 
      07-13-2008

"Erik Wikström" <(E-Mail Removed)> skrev i en meddelelse
news2mek.957$(E-Mail Removed)...
> On 2008-07-13 11:54, thomas wrote:
>> On Jul 13, 5:44 pm, Erik Wikström <(E-Mail Removed)> 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.

>
> Please do not quota signatures.
>
>> 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?


 
Reply With Quote
 
Erik Wikström
Guest
Posts: n/a
 
      07-13-2008
On 2008-07-13 18:48, rufus wrote:
> "Erik Wikstré—£" <(E-Mail Removed)> skrev i en meddelelse
> news2mek.957$(E-Mail Removed)...
>> On 2008-07-13 11:54, thomas wrote:
>>> On Jul 13, 5:44 pm, Erik Wikstré—£ <(E-Mail Removed)> 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.

>>
>> Please do not quota signatures.
>>
>>> 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
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      07-13-2008
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?= <(E-Mail Removed)> writes:
>>Is a reference not basically just a const pointer?

>That might be how the compiler vendors implements it (at least in some


You might think of reference parameters only.
But a reference also can be created as follows.

{ int v( 12 );
int & w( v );

Behind these two lines, there is no C++ statement you can write
that will behave differently for »v« and »w«. So in this case,
a reference is not like a pointer, but like a variable.
»w« is /bound/ to the same object as »v«. (Both are not pointers.)

I would go so far as to say, that here it makes no sense to
call »w« a »reference«. It /is/ a variable, because it can not
be distinguished from one after its creation.

The difference is the type of creation: »v« is declared with a
new object (with automatic storage duration), »w« is declared
without a new object (as an alias for »v«, using an
already-existing object).

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-13-2008
On Jul 13, 11:44 am, Erik Wikström <(E-Mail Removed)> 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,


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:(E-Mail Removed)
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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-13-2008
On Jul 13, 7:27 pm, Erik Wikström <(E-Mail Removed)> wrote:
> On 2008-07-13 18:48, rufus wrote:
> > "Erik Wikström" <(E-Mail Removed)> skrev i en meddelelse
> >news2mek.957$(E-Mail Removed)...
> >> On 2008-07-13 11:54, thomas wrote:
> >>> On Jul 13, 5:44 pm, Erik Wikström <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Difference between struct and class and pointer to member data Pep C++ 4 09-10-2009 09:35 AM
[pointers and arrays]: The difference between an array name and a pointer iamchiaweilin@gmail.com C Programming 7 10-02-2006 11:03 PM
old question: difference between pointer and reference Vols C++ 14 08-01-2006 06:07 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM



Advertisments