如果有课
class complex
{
private:
float real,imag;
public:
complex operator +(complex c)
{
complex t;
t.real=real+c.real;
t.imag=imag+c.imag;
return t;
}
如果我们调用重载运算符
c3=c1+c2;
然后编译器内部转换为 c3=c1.operator+(c2)
同样在运算符重载的类似示例中,= 的链接
class circle
{
private:
int radius;
float x,y;
public:
circle()
{}
circle(int rr,float xx,float yy)
{
radius=rr;
x=xx;
y=yy;
}
circle& operator=(const circle& c)
{
cout<<endl<<"assignment operator invoked";
radius=c.radius;
x=c.x;
y=c.y;
return *this;
}
int main()
{
circle c1 (10,2.5,2.5);
circle c2,c3;
c3=c2=c1;
c1.showdata();
c2.showdata();
c3.showdata();
return 0;
}
重载 = to 运算符将被调用 2 次,首先是 c2=c1,然后是 c3=c2。那么编译器将如何处理 c2=c1 及其函数原型??编译器将如何在内部转换这个重载的运算符 = 调用??(请参考上面的添加示例)谁的私有字段将被访问并返回到 ahich 对象值??