Pointer or reference?

D

davee

I have this function:

void update( MyType * m) {

updateMyType(m);

}

when 'update' returns the caller must see the changes made to m. But to
make this work correctly should 'update' not take a reference instead:

void update( MyType & m) {
....


When I use the first definition (using a pointer) its not always that
the content of 'm' is correct when 'update' returns. I assume there is
no such thing as a reference to a pointer?
 
V

Vladimir Jovic

davee said:
I have this function:

void update( MyType * m) {

updateMyType(m);

}

when 'update' returns the caller must see the changes made to m. But to
make this work correctly should 'update' not take a reference instead:

void update( MyType & m) {
...


When I use the first definition (using a pointer) its not always that
the content of 'm' is correct when 'update' returns. I assume there is
no such thing as a reference to a pointer?


It is not clear what exactly the update() method should do.
Anyway, your assumption is wrong. A reference to a pointer is shown in
this example:
void foo( int *&n )
{
++ *n; // increase value pointed by the pointer
++ n; // advance the pointer
}
int main()
{
int a[5];
int *p = &a[2];
foo(p);
}
 
D

davee

Vladimir said:
It is not clear what exactly the update() method should do.
Anyway, your assumption is wrong. A reference to a pointer is shown in
this example:
void foo( int *&n )
{
++ *n; // increase value pointed by the pointer
++ n; // advance the pointer
}

Ok so if I do:

void foo( int * n )
{
++ *n; // increase value pointed by the pointer
++ n; // advance the pointer
}

then 'n' will not have a defined value when foo returns?
 
D

davee

Stuart said:
Not sure what you mean by "have a defined value" here. The pointer you
passed in (i.e. the pointer of which the local pointer n is a copy)
won't be modified. On the other hand, the pointee (the int to which n
points) has been modified, and this will be visible outside the function
by dereferencing the pointer you passed in.

Stu



Lets assume that I have an object 'MyObject' with various fields. Below
foo1 and foo2 modifies the fields in a MyObject object:


void foo1(MyObject * mobj){
int value = 55;
myobj->setValue(value);
...
...

}



void foo2(MyObject *& mobj){
int value = 55;
myobj->setValue(value);
...
...

}



int main(){

MyObject * myobj = new MyObject();
foo1(myobj);
std::cout << "getValue = " << myobj.getValue() << std::endl;


foo2(myobj);
std::cout << "getValue = " << myobj.getValue() << std::endl;

}

As I understand its only when calling foo2 that the changes will visible
in main after returning from the call. The reason is that foo1 works
on a copy of the pointer which disappears when the function returns.

On contrary foo2 works on the original pointer. Or am I mistaken ?
 
F

Fred Zwarts

davee said:
Lets assume that I have an object 'MyObject' with various fields.
Below foo1 and foo2 modifies the fields in a MyObject object:


void foo1(MyObject * mobj){
int value = 55;
myobj->setValue(value);
...
...

}



void foo2(MyObject *& mobj){
int value = 55;
myobj->setValue(value);
...
...

}



int main(){

MyObject * myobj = new MyObject();
foo1(myobj);
std::cout << "getValue = " << myobj.getValue() << std::endl;


foo2(myobj);
std::cout << "getValue = " << myobj.getValue() << std::endl;

}

As I understand its only when calling foo2 that the changes will
visible in main after returning from the call. The reason is that
foo1 works
on a copy of the pointer which disappears when the function returns.

On contrary foo2 works on the original pointer. Or am I mistaken ?

Altough the second one works on a copy of the original pointer,
this copy points to the same object. So, it does not make a difference.
A real reference would not use any pointer at all:

void foo3(MyObject & mobj){
int value = 55;
myobj.setValue(value);
}

This will also modify the original object.
The advatange of using a reference here
is that the function does not need to test for a null pointer.
 
S

SG

Lets assume that I have an object 'MyObject' with various fields. Below
foo1 and foo2 modifies the fields in a MyObject object:

void foo1(MyObject * mobj){
   int value = 55;
   myobj->setValue(value);
   ...
   ...

}

void foo2(MyObject *& mobj){
   int value = 55;
   myobj->setValue(value);
   ...
   ...

}

int main(){
   MyObject * myobj = new MyObject();
   foo1(myobj);
   std::cout << "getValue = " << myobj.getValue() << std::endl;

   foo2(myobj);
   std::cout << "getValue = " << myobj.getValue() << std::endl;
}

For the record: MyObject is not an object but a type, specifically a
class-type.
As I understand its only when calling foo2 that the changes will visible
in main after returning from the call. The reason is that foo1 works
on a copy of the pointer which disappears when the function returns.
On contrary foo2 works on the original pointer. Or am I mistaken ?

Have you tried?

You don't seem to differentiate between the pointer and the pointee.
In the first version you pass a pointer by value, in the second by
reference. But "mobj" which in in both cases refers to a pointer to a
MyObject object is not modified so it does not matter which function
you call. Both functions will however mutate the object pointed to --
visibly. So, the expected output is

getValue = 55 (as set in foo1)
getValue = 55 (as set in foo2)

See "pointer fun with Binky" (C++ version)

Cheers,
SG
 
F

Fred Zwarts

davee said:
Lets assume that I have an object 'MyObject' with various fields.
Below foo1 and foo2 modifies the fields in a MyObject object:


void foo1(MyObject * mobj){
int value = 55;
myobj->setValue(value);
...
...

}



void foo2(MyObject *& mobj){
int value = 55;
myobj->setValue(value);
...
...

}



int main(){

MyObject * myobj = new MyObject();
foo1(myobj);
std::cout << "getValue = " << myobj.getValue() << std::endl;


foo2(myobj);
std::cout << "getValue = " << myobj.getValue() << std::endl;

}

As I understand its only when calling foo2 that the changes will
visible in main after returning from the call. The reason is that
foo1 works
on a copy of the pointer which disappears when the function returns.

On contrary foo2 works on the original pointer. Or am I mistaken ?

Since the pointer is not modified, it does not matter whether it is passed
by value or by reference.
It makes a difference however, when the object itself (not its pointer)
is used as a parameter:

void foo3(MyObject mobj){
int value = 55;
myobj.setValue(value);
}

void foo4(MyObject & mobj){
int value = 55;
myobj.setValue(value);
}

In foo3 the object is copied. The copy is modified.
The original object is not modified.

In foo4 the object is passed by reference.
There is no copy. So, the original object is modified.
 

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,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top