This is a really dumb question and for some reason I'm totally blanking on it right about now. Inside my main-loop I'd like to have a pointer-to-pointer that points to an Screen-object pointer ( So if the screen pointer is assigned a new object to point towards the pointer-to-pointer is updated along.

However, I'm getting an error when trying to assign my pointer-to-pointer the screen pointer:

The reason for this error is that the pointer returned by activeScreen is a temporary variable. It will be destroyed after this line (or maybe somewhere during the execution of this line, I'm not really sure about this). If you tried to take the address of this temporary variable, the resulting pointer will point to a non-existent variable (the temporary). This would (if you're lucky) lead to a crash of your program, or worse, nondeterministic behaviour. That's why taking the address of temporaries (or rvalues) isn't allowed.

That's also the reason why your other example works: You save the returned pointer to an lvalue (which you call p), of which it is perfectly valid to take to address. But be carefull, if you do this within a function and p is local to the function, if you return &p or store it somewhere, you will get all the problems that taking the address of an rvalue would have caused. So be carefull. The correct solution is what SiCrane proposes. Or as Bob said, are you sure you need a pointer to a pointer?

Everywhere you feel the need to store the pointer-to-pointer, why not instead store a pointer to ScreenManager? Then when you need access to the screen, you call the activeScreen() method on that pointer?

Having a method that provides a pointer to internal data of a class (i.e. the pointer to screen stored in ScreenManager) means you lose track of where that member is modified. Not a good thing.

However, even if their is another way I'd like to learn WHY my code don't work and HOW to make it work like this ( if possible ) , returning a pointer and assigning it to a pointer-to-pointer in a single call.

If it's bad code or "fishy" is not of relevancy right now ( I'v gotten a lot of good input right now )

EDIT:
If I sound ungrateful that is not the case, it's just that I want to know why it doesn't work and how to make it work, even if it's bad code.

When you return a pointer by value, you get a copy of the pointer. There is just no way to use a copy of an object to get the address of the original object. A copy is a brand new object with no address information of the original object. You need to either return the address of the object in the first place (the Screen ** approach) or a reference to the original object (the Screen *& approach).

When you return a pointer by value, you get a copy of the pointer. There is just no way to use a copy of an object to get the address of the original object. A copy is a brand new object with no address information of the original object. You need to either return the address of the object in the first place (the Screen ** approach) or a reference to the original object (the Screen *& approach).

Copy of the object? I'm guessing the object you are referring to is the pointer copy ( pointers are objects as well I guess ).

But then why does assigning it to a normal pointer THEN assigning that pointer to a pointer-to-pointer work?
Is it because the pointer copy still refers to the same value address so the "original" pointer gets the pointer-copys values adress then we the pointer-to-pointer can get the "original" pointers adress?

Because everything is return/pass by value? Is that why we only receive a copy of a pointer/copy of a reference object?

At the heart of it, everything is always "pass by value". When you pass in or return a reference or pointer, the value of this reference or pointer (an address) are passed by value and therefor copied.

Because everything is return/pass by value? Is that why we only receive a copy of a pointer/copy of a reference object?

At the heart of it, everything is always "pass by value". When you pass in or return a reference or pointer, the value of this reference or pointer (an address) are passed by value and therefor copied.

Yeah that is what I meant, though it's easy to forget that when dealing with pointers and references ( since I did ) in the beginning. Because they "act" like a magical solution that makes you modify the values from anywhere instead of just locally.

Nearly all books I'v read has failed to mention that EVERYTHING is at heart pass by value, especially when introducing pointers.