多继承纯基函数
multiple inheritance pure base functions
为什么不起作用?在BinaryOp类中定义虚拟函数GetNOperands()
和PerformOp()
并且在OpAdd类中定义DoOp()
。操作员类中的虚拟Prec()
函数也在OpAdd中定义。我研究了"钻石问题",我认为它不适用于此代码,因为派生类中的每个函数只有一个定义?这是代码:
#include <iostream>
#include <vector>
#define PREC_LOW 0
#include <assert.h>
//operator class - abstract
template <class T> class Op {
public:
virtual unsigned int GetNOperands() = 0;
virtual bool PerformOp( std::vector<T>& operands, T& result ) = 0;
virtual ~Op() {}
};
//binary operator class - for 2 operators - abstract
template <class T> class BinaryOp : public Op<T> {
public:
unsigned int GetNOperands();
bool PerformOp( std::vector<T>& operands, T& result );
virtual ~BinaryOp() {}
protected:
virtual bool DoOp( T first, T second, T& result ) = 0;
};
template <class T> class Operator : public Op<T> {
public:
virtual unsigned int Prec() = 0; //precedence
};
template <class T> class OpAdd : public BinaryOp<T>, public Operator<T> {
public:
unsigned int Prec();
private:
bool DoOp( T first, T second, T& result );
};
template <class T> unsigned int BinaryOp<T>::GetNOperands() {
return 2;
}
template <class T> bool BinaryOp<T>::PerformOp( std::vector<T>& operands, T& result ) {
assert( operands.size() == 2 );
return DoOp( operands.at(0),operands.at(1),result);
}
template <class T> bool OpAdd<T>::DoOp( T first, T second, T& result ) {
result = first + second;
return true;
}
template <class T> unsigned int OpAdd<T>::Prec() {
return PREC_LOW;
}
int main() {
OpAdd<int> a;
return 0;
}
编辑:编译器错误状态:
source.cpp: In function 'int main()':
source.cpp:55:13: error: cannot declare variable 'a' to be of abstract type 'OpAdd<int>'
OpAdd<int> a;
^
source.cpp:30:29: note: because the following virtual functions are pure withi
n 'OpAdd<int>':
template <typename T> class OpAdd : public BinaryOp<T>, public Operator<T> {
^
source.cpp:10:23: note: unsigned int Op<T>::GetNOperands() [with T = int]
virtual unsigned int GetNOperands() = 0;
^
source.cpp:11:15: note: bool Op<T>::PerformOp(std::vector<T>&, T&) [with T = int]
virtual bool PerformOp( std::vector<T>& operands, T& result ) = 0;
OpAdd<>
继承了BinaryOp<>
,但也继承了Operator<>
,它们都是抽象的。如果删除了不必要且不明确的后一个继承,则应该进行编译。
template <class T> class OpAdd : public BinaryOp<T> {
public:
unsigned int Prec();
private:
bool DoOp( T first, T second, T& result );
};
另一种方法,可能是避免定义Operator<>::Prec()
的最佳方法,是将继承设置为Op<>
虚拟,如下面的实时演示。
template <class T> class BinaryOp : public virtual Op<T>
...
template <class T> class Operator : public virtual Op<T>
....
错误消息告诉您问题的具体原因——通过AddOp<T>
的Operator<T>
基继承的GetNOperands
方法是抽象的。由于这是一个与BinaryOp<T>
基不同的基(有两个不同的Op<T>
基彼此无关),因此函数在基类的另一个实例中定义这一事实无关紧要。
你有两个选择如何解决这个问题:
-
将所有公共基类设为
virtual
,以便它们引用同一个基实例,而不是每次继承时都创建一个新实例。这使得继承以一种合理的方式工作(它在任何其他语言中都是如何工作的) -
只使用单一继承——每个类只能(直接)从一个基继承。这样可以避免生成基类的多个实例。
相关文章:
- C++虚函数继承
- 名称隐藏对静态函数继承的实例使用
- 派生类是从基类继承 v 指针并仅使用它,还是也有自己的 v 指针?
- C++ std::vector 中的虚拟析构函数继承
- 在基类中调用的 VIrtual 基函数,C++
- 从带有参数的基函数指针调用基方法
- C++ - 无法从基类继承 |提供空值
- C - 从基类继承,成员函数作为模板参数
- 从C++中的基类继承复制/移动构造函数作为构造函数
- 将从基类继承的构造函数与自定义构造函数混合使用
- 从只有纯虚拟函数的基类继承
- 使用继承的函数而不是基函数(不是抽象函数)
- C++类函数继承使用从基类继承的另一个类的变量
- C++如何确保在使用虚拟继承时调用继承的函数而不是基函数
- C++从具有相同虚拟函数名的多个基类继承
- 在C++11中显式删除了成员函数后,从不可编译的基类继承是否仍然值得
- 从模板基类继承构造函数而不重复模板参数
- 什么是从虚拟基类继承的虚函数的"virtual thunk"?
- 派生类的成员函数是否从基类继承虚拟性?
- 多继承纯基函数