多继承纯基函数

multiple inheritance pure base functions

本文关键字:基函数 继承      更新时间:2023-10-16

为什么不起作用?在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>基彼此无关),因此函数在基类的另一个实例中定义这一事实无关紧要。

你有两个选择如何解决这个问题:

  1. 将所有公共基类设为virtual,以便它们引用同一个基实例,而不是每次继承时都创建一个新实例。这使得继承以一种合理的方式工作(它在任何其他语言中都是如何工作的)

  2. 只使用单一继承——每个类只能(直接)从一个基继承。这样可以避免生成基类的多个实例。