=号操作符重载
#include <iostream>using std::cout;
using std::endl;
class base
{
public:
int a;
base & operator= (base const & a);
base(){}
base(base & a)
{
this->a=a.a+1;
cout<<this->a<<"复制构造函数调用"<<endl;
}
};
base &base::operator =(base const &a)
{
this->a=a.a+1;
cout<<this->a<<"赋值函数调用"<<endl;
return *this;
}
int _tmain(int argc, _TCHAR* argv[])
{
base b1;
b1.a=5;
base b2=b1;
getchar();
return 0;
}
1 结果显示 6复制构造函数调用 问为什么不调用赋值函数
2 赋值函数可以定义为 void 类名::operator =(类名 const &) 吗?