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条回答
我想做一个坏孩纸
2楼-- · 2019-05-26 16:41

thanks for the answers.. helped me a lot to understand the problem. In effect I posted some wrong code, because i was misunderstanding the real problem. Anyway, i think i partially solved my problem. Here's the code:

 #include <iostream>

``  using namespace std;

class Other{

public:

void foo(){
        cout << "Other\n";
}

void foo(int a){}
};

class Abstract : public Other{

public:
virtual void foo() {};
virtual void foo(int c){
        Other::foo(c);
}
};

class A : public Abstract{

public:

void foo(){
        cout << "A\n";
}
};

class B : public Abstract{

public:

void foo(){
        cout << "B\n";
}
};
int main(){

cout << "main\n";

Abstract * ptrA = new A();
Abstract * ptrB = new B();

Other *o = new Other();
o->foo();

ptrA->foo();
ptrB->foo();
ptrB->foo(3); //can't no more use the method foo with different signatures implemented in the base class Other, unless I explicitly redefined in the class Abstract
dynamic_cast<Other*>(ptrB)->foo(3);//can't dynamic_cast from derived to base

I was making two errors:

  1. In my real code (not the simplified version posted before) i forgot to declare virtual the function foo()

  2. Even declaring virtual wasn't enough. In fact all the implementations of that function must be wrapped inside the class Abstract to become visible to the subclasses A and b. Otherwise wont't compile.

I don't know if it could be a clean solution..in fact that way I need to wrap all foo method signatures.

查看更多
登录 后发表回答