Simple C++ question : setters

I am pretty new to C++ but not to programming in general and am pulling my hair out trying to figure out an issue. I have a customer class (Follower) that extends Mover. In Follower, I have a simple setter method:

Mover target;
void Follower::setTarget(Mover _target) {
    target = _target;
    
    cout << (&target == &_target) << endl;
}

My problem is, that the target passed in is not stored in my class instance. If you look at the identiy check after the assignment, it is returning false.

I have other code like this that works fine, but this is not working, and I have exhausted ideas of what might be wrong. I can provide more code if necessary, but I wanted to check if I was just missing something basic, or just misunderstanding something with C++.

Thanks for any input…

mike

When you write &variable you are actually retrieving the variable’s pointer in memory. So your check should only pass if you pass its own Follower::target as parameter.

Hi Mike,

In C++ when you pass in a paramter such as “Mover _target”, you are sending a copy of the original parameter. This copy is a total different object.

Then, in the body of the setTarget method, you are copying (target = _target) the object again to your internal Mover “target” member. These copys build new objects, thus the pointers are not the same as they point to different objects.

I assume you just want to maintain a reference/pointer to the original target set in setTarget, so you should use pointers instead of copying the object.

This is a usual confusion because other languages use pointers directly under the hood when writing the parameters as setTarget(Mover _target /pointer really/).

So:

Mover * target;
void Follower::setTarget(Mover * _target) {
    target = _target;
    
    cout << (target == _target) << endl;
}

And in the call:

Mover oneTarget;
Follower follower;
follower.setTarget(&oneTarget);

Just a warning, be careful with the lifetime of the Mover oneTarget, as follower will reference it unknowing if the object is still alive.

1 Like

Thank you so much. THis got it working. I need to go read up a bit more on pointers.

mike

A funny way to start on pointers https://www.youtube.com/watch?v=UvoHwFvAvQE

Just to follow up on this, for anyone who runs into a similar issue in the future, I found that this article in ofBook covers all of the basics in a clear and concise manner:

http://openframeworks.cc/ofBook/chapters/memory.html

Here are a couple of notes I took from the chapter that cover my initial question:

& : reference operator : gives the memory address of variable

  • : deference operator : gives the value of the variable (pointed to by the pointer). Dereferences a reference.
    -> : used to access a vaiable within a pointer (equivilant to (*p).x)

Doing a = b actually makes a copy, its not a reference.

When passing objects to functions as parameters, they are copied by default.

To pass an argument by reference, use &

void foo(vector & ps){}

If you want to pass by reference, but also ensure the passed in object is not modified, you can use const

void foo(const vector & ps){}

You can return a reference from a function, but in general, it can be a bad idea (often because the variable / memory will be deleted from memory when it leaves the scope of the function call.

mike

3 Likes