main.cpp:40: warning: taking address of temporary

I received this error while checking for collisions between my car and all the enemy cars:

SDL_Rect* playerRect = &player.retrnRect();

retrnRect() returns the coordinates of player.

I am confused, what does it mean by "temporary"
Last edited on
I assume retrnRect() returns type SDL_Rect, correct?

The return value is temporary and exists only for assignment to some other variable. As soon as you reach the next semicolon, the return value no longer exists.

Returned values are just like "by value" parameters. You're not actually returning the same rectangle, you're returning a copy of it. Unless you assign that copy to something, it vanishes immediately after the function call.
player.retrnRect() is returning a variable that is local to that function.
That variable is therefore "temporary" in that it will be destroyed as
soon as the assignment takes place. You are taking the address of
an object that is going to be destroyed as soon as you take its
address.
Okay, got it.
Topic archived. No new replies allowed.