Hello all I have this example. Usually in this case I would use Visitor pattern. However for some reason the person who wrote Base
, DerivedA
, DerivedB
prefers dynamic_cast
s. Keep in mind that I can not change Base
, DerivedA
, DerivedB
classes.
I have got around casting with partial specialization. Please let me know if this is a good solution or there is a better one?
#include <iostream>
using namespace std;
class CBase{};
class CDerivedA: public CBase{};
class CDerivedB : public CBase{};
class CDerivedC : public CBase{};
template <typename T>
void Draw(T* face)
{
cout<<"Base"<<endl;
}
template <>
void Draw<>(CDerivedA* face)
{
cout<<"A"<<endl;
}
template <>
void Draw<>(CDerivedB* face)
{
cout<<"B"<<endl;
}
int main() {
CDerivedA* a = new CDerivedA ();
CDerivedB* b = new CDerivedB ();
CDerivedC* c = new CDerivedC ();
Draw(a);
Draw(b);
Draw(c);
return 0;
}