I have a tool that's been subclassed. A bug has been reported where the subclass has taken a value from the superclass it shouldn't have taken: mySuperclass.SetMyVar()
is being called for a subclass object.
If I can't make SetMyVar
private
– because it is being called in initialisation that happens for both classes – can I make the behaviour dependent on whether this is a superclass item or subclassed item?
The fix suggested – to supply SetMyVar()
for the subclass – presents maintenance issues should the superclass be derived from again.
[Still, if I used b.SetMyVar()
and it didn't do it then I might have another bug to contend with... maybe the subclass implementation is right.]
Something like this:
class A
{
public:
A() {myVar = 20; }
void SetMyVar(int val)
{
//eg (typeid (this).name() == "class A*") // doesn't work
//eg (dynamic_cast <A*>(this)) // doesn't work
[something here to make it happen for only objects of type superclass]
{
myVar = val;
}
}
int myVar;
};
class B : public A
{
public:
B() { myVar = 10; }
};
int main(...)
{
A a;
B b;
a.SetMyVar(2);
b.SetMyVar(3); < -- this should not set myVar
std::cout << a.myVar << " : " << b.myVar;
return 0;
}