I have a base class and a child class extending it. The child class has its own method inside that the parent class does not have. That is, declaring it as virtual in the base class is not really an option.
class A {
public:
virtual void helloWorld();
};
class B : public A {
public:
virtual void helloWorld();
void myNewMethod();
};
Then, in my implementation, I have a pointer to A and I constructed it as B:
// somewhere in a .cpp file
A* x;
x = new B();
x->myNewMethod(); // doesn't work
My current solution is to cast it:
((B *)x)->myNewMethod();
My question is, is there a cleaner way of doing this, or is casting the way to go?
See Question&Answers more detail:os