幫助?我真的不知道這裏發生了什麼? 爲什麼在分配的第3行中,當B分配給B時,它會調用A的operator =?C++ operator =怪異behviour
class A{
public:
A& operator=(const A&){cout << "A assignment" << endl;return *this;}
};
class B:public A{
public:
A& operator=(const A&){cout << "B assignment" << endl;return *this;}
};
int main() {
A a;
B b;
B b2;
a=b; // output: A assignment
b=a; // output: B assignment
b=b2; // output: A assignment WHY??
return 0;
}
[不編譯](http://ideone.com/TWtPTP) –
這樣編譯? –
@jafar查看我之前的評論。 –