View Single Post
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 03-05-2011 , 08:06   Re: Accessing Private Data
Reply With Quote #4

Basically, the code will break when the position of the m_LangId field relative to the beginning of the CPlayer object in memory changes.

This could happen if someone changes the class or if it is compiled with other alignment options.

Another situation that may happen would be a change in PlayerManager, so that it would not be returning CPlayer for some cases, but an instance of another class, for example a new class CBotPlayer, which would also inherit from IPlayerManager.

I guess that none of this is likely to happen, but it could ;)

Greetings,
PM

Edit:
Just for completeness, a reason why the code using a self-defined struct could have failed:
If a class itself or a class it inhertis from contains virtual functions, the first 4 (x86) or 8 (x64) bytes of the object's data will be a pointer to the 'virtual function table'. This is automatically added by the compiler and is used to resolve virtual function calls (ie. to make sure that if you call GetLanguageId on a IGamePlayer * variable, it will actually call CPlayer::GetLanguageId if the real object is a CPlayer).
If you re-create the class yourself and don't have any virtual functions (here they come from inheriting from IGamePlayer), the member variable positions won't match the member variable positions in the real class with a virtual function table pointer, and you'll be overwriting something else.
__________________
hello, i am pm

Last edited by PM; 03-05-2011 at 08:14.
PM is offline