difference between pointer and reference

T

thomas

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?
 
E

Erik Wikström

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.
 
T

thomas

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.
It's correct in my understanding.
Any more details to clarify this?
 
E

Erik Wikström

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.
 
R

rufus

Erik Wikström said:
Please do not quota signatures.


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?
 
E

Erik Wikström

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.
 
S

Stefan Ram

=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?= said:
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).
 
J

James Kanze

On 2008-07-13 10:56, thomas wrote:
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.)
 
J

James Kanze

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top