I can't understand this "const*&" stands for?

H

hoox2

void push_front(Node const*& head, int data);


Can someone tell me what it means for "const*&"? A reference or a pointer?
 
J

Jakob Bieling

hoox2 said:
void push_front(Node const*& head, int data);


Can someone tell me what it means for "const*&"? A reference or a pointer?

Both, sort of *g* Read everything from right to left: 'head' is a
reference to a pointer to a constant 'Node'. This means the passed pointer
can be modified by the function, but the object the pointer points to cannot
be modified.

hth
 
D

David B. Held

hoox2 said:
void push_front(Node const*& head, int data);


Can someone tell me what it means for "const*&"? A
reference or a pointer?

Except for an initial const, these kinds of types should be
understood from left to right:

Node const x; // a const object of type Node
Node const* x; // a pointer to a const Node object
Node const* &x; // a reference to a pointer to a const Node

My guess is that push_front() is an operation that works
on lists, which is probably why it takes head by reference.
Since the function probably doesn't need to modify the
pointed-to Node object, it takes that as const. Since it
is probably going to modify the head pointer, it takes
that as non-const &.

Dave
 
J

Jonathan Mcdougall

void push_front(Node const*& head, int data);
Can someone tell me what it means for "const*&"? A reference or a pointer?

A const reference to a pointer. The function probably reset your pointer
to 0, meaning it takes ownership of the object, though the name 'head' makes
me wonder about that.


Jonathan
 
H

Howard

Jonathan Mcdougall said:
pointer?

A const reference to a pointer. The function probably reset your pointer
to 0, meaning it takes ownership of the object, though the name 'head' makes
me wonder about that.

Nope...it's a reference to a pointer to a constant Node object, not a const
reference.

The const keyword could go in front of the Node and it would be the same,
but otherwise read it from right-to-left to get:
reference-to-pointer-to-const-Node.

-Howard
 
J

Jonathan Mcdougall

void push_front(Node const*& head, int data);
Nope...it's a reference to a pointer to a constant Node object, not a const
reference.

The const keyword could go in front of the Node and it would be the same,
but otherwise read it from right-to-left to get:
reference-to-pointer-to-const-Node.

You're right, sorry.


Jonathan
 

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

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,829
Latest member
PIXThurman

Latest Threads

Top