小编bin*_*157的帖子

运算符+在基类中重载并在派生类中使用它

我有2个类,base(带有复制构造函数)和派生,在baseΙ有重载operator+:

class Base {

  public:

     Base(const Base& x) {
         // some code for copying
     }

     Base operator+(const Base &bignum) const {
         Base x;
         /* ... */
         return x;
     }
};

class Derived : public Base {
};
Run Code Online (Sandbox Code Playgroud)

当我尝试做那样的事情

Derived x;
Derived y;
Derived c=x+y;
Run Code Online (Sandbox Code Playgroud)

我得到错误:conversion from "Base" to non-scalar type "Derived" derived 问题可能在于该运算符+返回Base类型的对象,我想将它分配给Derived类型对象?

c++ inheritance operator-overloading

2
推荐指数
1
解决办法
5848
查看次数

标签 统计

c++ ×1

inheritance ×1

operator-overloading ×1