小编Mon*_*ner的帖子

赋值运算符在派生类中不可用

基类中的赋值运算符似乎在派生类中不可用.鉴于此代码:

#include <iostream>

class A{
    int value;
public:
    A& operator=(int value){
        this->value = value;
        return *this;
    }
};

class B : public A{};

int main(){
    B b;
    b = 0; // Does not work
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

GCC 6.4说:

错误:'operator ='不匹配(操作数类型为'B'和'int')

怎么了?

c++ inheritance assignment-operator

30
推荐指数
4
解决办法
1409
查看次数

标签 统计

assignment-operator ×1

c++ ×1

inheritance ×1