挽救C++程序员生命之operator=(熟悉C++标准的一定要进啊!)
关于问题的起因见:
http://expert.csdn.net/Expert/topic/2777/2777264.xml?temp=8.672732E-02
我答了已后还心神不安.今天写了一个例程,果然抓狂:
#include <iostream>
class base
{
public:
virtual base&operator=(const base&right)
{
std::cout <<"in base"<<std::endl;
return *this;
}
};
class child:public base
{
public:
virtual base&operator=(const base&right)
{
std::cout <<"in child"<<std::endl;
return *this;
}};
int main(int argc, char* argv[])
{
child one;child two;
one=two;
}
看到了吧!one=two会调用base的!看来operator=的特殊还不只"只能是非静态成员函数","编译器会自动产生","不能被子类继承".有人能解释上面的原由吗?