Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualBacterius

Posted 01 January 2013 - 10:59 PM

I understand the problem now, thank you.
The call to the function was this:

CStateManager::change_state(this, nextState);

How can I modify it so that "this" is passed by reference? (If it's possible at all)

 

 

Ah, I see. No, it is illegal to modify "this", at least in C++. This means your design has failed, and you need to rethink it. Why do you need different classes for State_Main_Menu, State_Exit, etc...? Is an enum flag not enough? What are you trying to do, exactly? It looks like you are trying to implement a state machine for your GUI, but this is clearly not the right approach here, as the language is very clearly telling you "don't do this".


#2Bacterius

Posted 01 January 2013 - 10:58 PM

I understand the problem now, thank you.
The call to the function was this:

CStateManager::change_state(this, nextState);

How can I modify it so that "this" is passed by reference? (If it's possible at all)

 

Ah, I see. No, it is illegal to modify "this", at least in C++. This means your design has failed, and you need to rethink it. Why do you need different classes for State_Main_Menu, State_Exit, etc...? Is an enum flag not enough? What are you trying to do, exactly?


#1Bacterius

Posted 01 January 2013 - 10:57 PM

<blockquote class="ipsBlockquote" data-author="Bacterius" data-cid="5016564"><p>You have a scope problem. The value of your pointer, cur_state, being an argument, only exists within this function, and will be forgotten once you return from it (while any reference it was pointing to, will not). So, the delete instruction will destroy the reference the pointer is pointing to, and the new instruction will correctly create a new instance and make cur_state point to it.<br />&nbsp;<br />However, as soon as you return from the function, the value of the cur_state pointer (which is, the address of the instance you just allocated) will be lost, and the caller will be left with the old value, which was the address of the instance you just deleted a few lines earlier. This is, in essence, because you passed the pointer by value and not by reference. The solution is to pass the pointer by reference, or pass a pointer to the pointer, or return the new pointer instead.<br />&nbsp;<br />Basically, the thing to understand is that you did <em>not</em> pass cur_state to your change_state&nbsp;function, but only its <em>value</em>. Whatever pointer variable you passed as an argument to this function, will be left unchanged, a copy of it will be made, and discarded after the function returns.&nbsp;The unfortunate result being, that <u>the pointer to "new State_Exit()" never made it beyond the "return" statement</u>. As soon as the function returned, the copy holding the new, valid pointer was discarded, and you kept on going with the old value, which now points to a deleted instance.<br />&nbsp;<br />The new State_Exit() object is there, well and alive. You just don't know where it is, and have no reference to it anywhere in your code and no way to access it beyond the function where it was created, because you lost your pointer to it after said function returned.<br />&nbsp;<br />Try making cur_state a reference to a pointer instead, that way you will be affecting the real cur_state variable and not just a copy, and your code should work. You can also use a pointer to a pointer (so that you can now affect the inner pointer outside the function too, as well as the object, which is pointed to by the pointed pointer), but this is more C-ish and more error-prone.</p></blockquote><br />I understand the problem now, thanks you.<br />The call to the function was this:<br /><br />CStateManager::change_state(this, nextState);<br /><br />How can I modify it so that "this" is passed by reference? (If it's possible at all)

Ah, I see. No, it is illegal to modify "this", at least in C++. This means your design has failed, and you need to rethink it. Why do you need different classes for State_Main_Menu, State_Exit, etc...? Is an enum flag not enough? What are you trying to do, exactly?


PARTNERS