I have the following classes, class A with a method, and a container class B:
class A
{
void foo();
};
class B
{
A * m_a;
void setA(A* a)
{
m_a = a;
}
void callFoo()
{
m_a->foo();
}
};
So now I want to extend the functionality of A to BarA adding fooBar(), so I also have to create a BarB to call this new method.
class BarA : public A
{
void fooBar();
};
class BarB : public B
{
void callFooBar()
{
// I know for sure its a BarA, but its saved as an A pointer
BarA * barA = dynamic_cast<BarA*>(m_a)
barA->fooBar();
}
};
Now we create the caller:
class LetsFooBar
{
BarA barA;
BarB barB;
void foobar()
{
barB.setA(&barA);
barB.callFooBar();
}
}
Everything works OK but there is the dynamic_cast issue in the pattern, as well as needing to extend the B class when wanting to extend the A class.
I would like to find a more elegant way to solve this issue.
Thank you