C++ and inheritance in abstract classes

2019-05-26 16:14发布

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?

7条回答
SAY GOODBYE
2楼-- · 2019-05-26 16:32

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).

查看更多
一夜七次
3楼-- · 2019-05-26 16:32

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 and ptrB of type OtherClass or AbstractClass in your lower example?

If they are OtherClass I would expect the behaviour you described. You could try casting the pointer to AbstractClass then:

dynamic_cast<AbstractClass*>(ptrA)->anyMethod();

查看更多
贪生不怕死
4楼-- · 2019-05-26 16:32

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 as virtual

查看更多
欢心
5楼-- · 2019-05-26 16:38

Why don't you do:

class OtherClass 
{
    public:
    virtual void anyMethod()
    {
       cout << "OtherClass";
    };
}

That should solve your problems

查看更多
Bombasti
6楼-- · 2019-05-26 16:40

I think that if the method in OtherClass that you want to override in A and B is NOT virtual, then the override is not implicit.

I believe there's a way to Explicitly override the functions, look that up.

查看更多
劫难
7楼-- · 2019-05-26 16:41

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.

查看更多
登录 后发表回答