Pointer question

B

bilaribilari

Hi,
I came across a function which looks like this:

void func ( Object *& param1 )
{
....
}

Can someone please explain what does *& mean? Where can I get a good
understanding of 'pointer to reference' and 'reference to a pointer' if
any such thing exists. Whats the purpose of doing something like this?

Also, if I have a function that is like this:

void func ( Object * param1 )
{
....
param1 = new Object ();
....do something to param1
}

int main ()
{
Object * x = new Object () ;
func ( x );

Object x ;
func ( &x );
}

What happens to x in the above cases?
Thanks a lot in advance.
Best regards,
B.
 
J

John Harrison

Hi,
I came across a function which looks like this:

void func ( Object *& param1 )
{
...
}

Can someone please explain what does *& mean? Where can I get a good
understanding of 'pointer to reference' and 'reference to a pointer' if
any such thing exists.

There no such thing as a pointer to a reference in C++. No reason in
prinicple that you couldn't have such a thing, its just C++ that does
not allow it.

A reference is a C++ type that refers to some object, if that object is
an int then you have a reference to an int, if that object is a pointer
then you have a reference to a pointer.

Whats the purpose of doing something like this?

Sometimes you want a function to modify one of the parameters that is
passed to it. A reference is one way of doing this. If the thing you
want to change is an int you would use a reference to an int

void change_me(int& me) // int version
{
...
}

If the thing is a pointer (to char say) you would use a reference too,
only this time its a reference to a pointer.

void change_me(char*& me) // pointer version
{
...
}

Basically there is nothing magic about pointers, they are values just
like ints or doubles or whatever, and so you can have references to
pointers just like anything else.
Also, if I have a function that is like this:

void func ( Object * param1 )
{
...
param1 = new Object ();
...do something to param1
}

int main ()
{
Object * x = new Object () ;
func ( x );

Object x ;
func ( &x );
}

What happens to x in the above cases?

Nothing at all. All that func changes is param1 not x. param1 is just a
copy of x, the original x in main is unchanged. Of course it would be
different if you used a reference.

Compare with integers.

void func ( int param1 )
{
param1 = 2;
}

int main()
{
int x = 1;
func(x);
func(3);
}

In this code would you expect the value of x to change? Of course not,
so why would it be any different with a pointer. There is nothing magic
about pointers.

john
 
B

benben

Hi,
I came across a function which looks like this:

void func ( Object *& param1 )
{
...
}

Can someone please explain what does *& mean? Where can I get a good
understanding of 'pointer to reference' and 'reference to a pointer' if
any such thing exists. Whats the purpose of doing something like this?


Object*& param1 means reference to a pointer to an object of type Object.

Also, if I have a function that is like this:

void func ( Object * param1 )
{
...

Provided the above ... doesn't include a delete operation you are very
likely to have a memory leak.
param1 = new Object ();
...do something to param1
}

int main ()
{
Object * x = new Object () ;
func ( x );

Object x ;
func ( &x );
}

Effectively the same as:

int main()
{
Object* x = new Object();

x = new Object();
// do something to x

x = new Object();
// do something to x
}
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top