65,210
社区成员
发帖
与我相关
我的任务
分享
class Base{
virtual Base& operator=(const Base&aa){
};
class Derived: public Base{
Derived& operator=(const Base&aa){
cout<<"Derived operator= Base"<<endl;
return *this;
}
}
哦,忘了,它们的参数可以相同,我再去想想,感谢指点Derived& operator=(const Base&aa);
而不是Derived& operator=(const Derived&aa);
下边是测试的代码
#include <iostream>
using namespace std;
class Base{
public:
virtual Base& operator=(const Base&aa){
cout<<"Base operator= Base"<<endl;
return *this;
}
};
class Derived: public Base{
public:
Derived& operator=(const Base&aa){
cout<<"Derived operator= Base"<<endl;
return *this;
}
Derived& operator=(const Derived&aa){
cout<<"Derived operator= Derived"<<endl;
return *this;
}
};
int main(){
Base b1;
Derived d1, d2;
Base &br = d1;
br.operator=(b1);
br = d2;
d1 = d2;
return 0;
}