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

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

问题描述

我有一个问题在正确处理方法覆盖的抽象类存在
在我的类层次结构。
我将尝试解释:

  class AbstractClass {
public:
virtual void anyMethod()= 0;
};

class A:public AbstractClass {
void anyMethod(){
//任何方法的实现
cout< 一个;
}
};

class B:public AbstractClass {
void anyMethod(){
// B实现anyMethod
cout< B;
}
};

AbstractClass * ptrA,* ptrB;

ptrA = new A();
ptrB = new B();
ptrA-> anyMethod(); // prints A
ptrB-> anyMethod(); // prints B

确定...上一个示例工作正常。AbstractClass的具体实现
方法anyMethod将在运行时被调用。
但是AbstractClass派生自另一个基类,它有一个方法不是虚拟的
调用anyMethod:

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

class AbstractClass:public OtherClass {
public:
virtual void anyMethod()= 0;
};

// A和B声明的方式与前面描述的相同。

现在,如果我尝试这样:

  ptrA = new A(); 
ptrB = new B();
ptrA-> anyMethod(); // prints OtherClass
ptrB-> anyMethod(); // prints OtherClass

我误解了什么?
有没有使用ptrA和ptrB打印A和B而不使用cast,typeid等的解决方案?

解决方案

感谢答案..帮助我很多了解问题。
实际上我发布了一些错误的代码,因为我误解了真正的问题。
无论如何,我想我部分解决了我的问题。
这里是代码:

  #include< iostream> 

``using namespace std;

class其他{

public:

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

void foo(int a){}
};

类摘要:public其他{

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

class A:public Abstract {

public:

void foo(){
cout< 一个;
}
};

class B:public Abstract {

public:

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

cout<< main \\\
;

抽象* ptrA = new A();
抽象* ptrB = new B();

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

ptrA-> foo();
ptrB-> foo();
ptrB-> foo(3); //不能再使用在基类中实现的具有不同签名的方法foo其他,除非我在类中明确地重新定义了抽象
dynamic_cast< Other *>(ptrB) - > foo(3); //不能dynamic_cast从派生到基本

我有两个错误:


  1. 在我的实际代码中(不是之前发布的简化版本)我忘了声明虚拟
    函数foo


  2. 甚至声明虚拟是不够的。事实上,该函数的所有实现必须包装在类Abstract中,以使子类A和b可见。否则不会编译。


我不知道它是否可以是一个干净的解决方案。方式我需要包装所有foo方法签名。


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?

解决方案

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.

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

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