クラスがあれば
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 演算子は、最初に c2=c1 に対して、次に c3=c2 に対して 2 回呼び出されます。次に、コンパイラは関数プロトタイプで c2=c1 をどのように扱いますか??コンパイラはこのオーバーロードされた operator = call を内部的に変換する方法を教えてください? ??