Wondering how this code worked
I have a base class called eg MyBase. It has a private member called
myprivatemember which is of course not accessible from users of the
class.
But I suddenly need to update the variable myprivate and I am not
allowed to edit the original source code so I create a new class,
MyNewToAllowChange which implements a public function called
SetMyPrivate which allows me to change the variable.
I realise this is bad design etc but just bear with me.
I don't implement operator= (or assign) and in my code I have an
instance of the base object - MyBase.
I need to update the myprivatemember and so the following functions
have been provided which make it possible.
inline MyNewToAllowChange* Mt(MyBase* thebase)
{
return static_cast<MyNewToAllowChange*>(thebase);
}
I can then do this sort of thing:
(if my base object is called mybaseobj (a pointer))
Mt(mybaseobj)->SetMyPrivate(whatever);
MyBase
MyNewToAllowChange
How is this working? Is it a fairly standard technique?
I can see that it is useful in certain circumstances, just not see
before.