C++ 和抽象类中的继承 [英] C++ and inheritance in abstract classes

查看:28
本文介绍了C++ 和抽象类中的继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在正确处理存在抽象类的方法覆盖时遇到问题在我的类层次结构中.我会尽力解释:

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

好的..前面的例子工作正常..AbstractClass的具体实现方法 anyMethod 将在运行时调用.但是 AbstractClass 派生自另一个基类,该基类的方法不是虚拟的调用任何方法:

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

我误会了什么?有没有办法让ptrA和ptrB在不使用cast、typeid等的情况下打印A和B?

What am I misunderstanding? Is there any solution for making ptrA and ptrB printing A and B without using cast, typeid, etc?

推荐答案

感谢您的回答.. 帮助我理解了这个问题.实际上,我发布了一些错误的代码,因为我误解了真正的问题.无论如何,我想我部分解决了我的问题.代码如下:

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
";
}

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
";
}
};

class B : public Abstract{

public:

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

cout << "main
";

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

我犯了两个错误:

  1. 在我的真实代码(不是之前发布的简化版本)中,我忘记声明 virtual函数 foo()

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

即使声明虚拟还不够.事实上,该函数的所有实现都必须包装在抽象类中,才能对子类 A 和 b 可见.否则无法编译.

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.

我不知道这是否是一个干净的解决方案......事实上,我需要包装所有 foo 方法签名.

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

这篇关于C++ 和抽象类中的继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆