Pointer to Pointer blackout question

Started by
12 comments, last by PureSnowX 11 years, 3 months ago

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:


Screen* ScreenManager::activeScreen()
{
  return m_screen;
}

Screen** s = &screenManager.activeScreen();

I'm getting the following error:

Error: expression must be an lvalue or function designator


However this works just fine:


Screen* p = screenManager.activeScreen();
Screen** s = &p;

I'm sure this is VERY simple, but I just keep blanking where the problem lies.

Advertisement
I think you want:

Screen** ScreenManager::activeScreen()
{
  return &m_screen;
}

I think you want:


Screen** ScreenManager::activeScreen()
{
  return &m_screen;
}

Is this the way to do it? Because I'd rather avoid returning a pointer-to-pointer seeing as other functions just use a normal pointer.

What do you want to accomplish with a double pointer that you cannot do with a single pointer?


Screen* s = screenManager.activeScreen();
Screen** s = &screenManager.activeScreen();


Error: expression must be an lvalue or function designator

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?
Well you could also return a reference to the pointer. Then other callers that don't need the address of the member won't need different syntax.

Screen *& ScreenManager::activeScreen() {
  return m_screen;
}
Either way you need a pointer or reference to the actual pointer you want to track changes to.
This sounds dodgy to me.

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.
Thanks all your suggestions are appreciated!

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?

Is it something like that?

This topic is closed to new replies.

Advertisement