【发布时间】:2017-10-04 03:26:55
【问题描述】:
C++ Complete Reference 说,“除了 = 运算符,运算符函数由派生类继承。”
但我无法理解以下代码的行为:
#include<iostream>
using namespace std;
int main(){
class b{
int i;
public:
int operator=(b parm){
cout<<"base overload";
};
};
class d: public b{
int j;
public:
};
b inst1,inst11;
d inst2,inst22;
int a;
inst1=inst11; //works because assignment operator is overloaded for b
inst2=inst22; //If =operator function is not inherited then why does it output "base oberload"
inst1=inst2; //works because assignment overloaded for b
// inst2=inst11; //But if b was inherited then this should also work but it doesnt
}
我期待两个输出语句“基本过载”,但它输出三个为什么?这让我发疯了
【问题讨论】:
标签: c++ inheritance operator-overloading