Valleriani
Valleriani

Reputation: 193

Proper way to pass a pointer? c++

I have a simple question that I am not 100% sure on.

Let us say I have a Entity class, that handles objects on the screen. Let us say the Entity class has two float variables, 'x' and 'y' (aka coordinates). Also let us say the entity I am passing has already been declared and is in memory.

I have another class that handles camera movement. It requires an entity to center on. The entity that it is centered on can changed, so I need to use a pointer here I believe. The only thing I do here is grab the X and Y variables when needed. Nothing is changed here.

I've defined it as

void StarField::ChangeFollowEntity(Entity* newFollowEntity) {
    followEntity = newFollowEntity;
}

where followEntity is also an Entity class. I would call ChangeFollowEntity(..) to change the entity. Is this actually correct?

I've also seen however this:

void StarField::ChangeFollowEntity(Entity newFollowEntity) {
    followEntity = &newFollowEntity;
}

In both cases followEntity is defined as Entity* followEntity; .. What does the second example exactly do here? From what I understand, & would typically be used as a reference type. Maybe it is incorrect to do to begin with.

I am pretty sure I shouldn't be using a reference in this case because the followEntity changes, which references I believe cannot change and must be defined.

So my question is, is my first example correct and the right way to do it? What does the second example do exactly?

Upvotes: 0

Views: 139

Answers (3)

user007
user007

Reputation: 2172

In your second version ::

void StarField::ChangeFollowEntity(Entity newFollowEntity) {
    followEntity = &newFollowEntity;
}

You pass the newFollowEntity by value, so when your function is called from main or from where ever! A copy of the object is made (using the copy constructor) and sent to the function ChangeFollowEntity, and after the completion of the execution of the function, your followEntity has the address of the COPY of the object, which gets destroyed after the completion of the function call, so your pointer followEntity is left dangling, which is undefined behavior if you access any entity using the followEntity

First is the correct way to do it!!

Upvotes: 3

Drew Dormann
Drew Dormann

Reputation: 63946

What does the second example exactly do here?

Your second example creates a Entity newFollowEntity which only exists for the duration of that function call.

The address of that variable is stored, and then the variable is destroyed, leaving a dangling pointer.

That's bad.

I am pretty sure I shouldn't be using a reference in this case because the followEntity changes, which references I believe cannot change

You can use a reference in this case. A referenced object can be changed - you are probably recalling that a reference cannot be reassigned.

Upvotes: 1

Alexander Balabin
Alexander Balabin

Reputation: 2085

The second example does the following:

  1. When the function is called a new Entity object newFollowEntiry is created and whatever is passed into the function is used to construct it
  2. Then an address of that local stack based object is taken and assigned to followEntiry to be stored presumably.
  3. When the function execution is complete newFollowEntiry is destroyed
  4. The folowEntity pointer is now pointing at a location in stack where there is not Entity object anymore.
  5. BUG

Upvotes: 1

Related Questions