i have a problem in properly handling method overriding where an abstract class is present inside my classes hierarchy. I'll try to explain:
class AbstractClass{
public:
virtual void anyMethod() = 0;
};
class A : public AbstractClass {
void anyMethod() {
// A implementation of anyMethod
cout << "A";
}
};
class B : public AbstractClass {
void anyMethod() {
// B implementation of anyMethod
cout << "B";
}
};
AbstractClass *ptrA, *ptrB;
ptrA = new A();
ptrB = new B();
ptrA->anyMethod(); //prints A
ptrB->anyMethod(); //prints B
Ok..previous example work fine .. the concrete implementation of the AbstractClass method anyMethod will be called at run time. But AbstractClass is derived from another base class which has a method not virtual called anyMethod:
class OtherClass {
public:
void anyMethod() {
cout << "OtherClass";
}
};
class AbstractClass : public OtherClass {
public:
virtual void anyMethod() = 0;
};
//A and B declared the same way as described before.
Now , if i try something like that:
ptrA = new A();
ptrB = new B();
ptrA->anyMethod(); //prints OtherClass
ptrB->anyMethod(); //prints OtherClass
What am I misunderstanding? Is there any solution for making ptrA and ptrB printing A and B without using cast, typeid, etc?
If
anyMethod
was declared virtual in the base class to which you have a pointer or reference, it should be looked up virtually and print A and B correctly. If it wasn't, then there is nothing you can do (beyond changing it to be virtual).DeadMGs answer is of course correct. But, if you cannot change
OtherClass
Methode (e.g. it's from a third party lib) you might want to try this:Are the pointers
ptrA
andptrB
of typeOtherClass
orAbstractClass
in your lower example?If they are
OtherClass
I would expect the behaviour you described. You could try casting the pointer toAbstractClass
then:dynamic_cast<AbstractClass*>(ptrA)->anyMethod();
As far as I can see from your code
OtherClass::anyMethod()
is not a virtual and already implemented. It should work as you described if you define it asvirtual
Why don't you do:
That should solve your problems
I think that if the method in
OtherClass
that you want to override inA
andB
is NOT virtual, then the override is not implicit.I believe there's a way to Explicitly override the functions, look that up.
I think that your declaration for the second case is: OtherClass* ptrA; and not AbstractClass *ptrA; if you declared like the first case , there's no problem because the method is virtual,but if you declare as OtherClass , the compiler will not find virtual and bind to the adress of this method without using vtble.