C
cronusf
Suppose I have a class Complex and I overload operator double() to get
the real part. I also define:
std:stream& operator<<(std:stream& os, Complex& complex)
{
os << "(" << complex.real<< ", " << complex.imaginary << ")";
return os;
}
Now if I write:
Complex a(1.0, 2.0)
Complex b(1.0, 2.0)
cout << a + b << endl;
I have overloaded operator+ for Complex.
However, when I write this, it calls operator double() and outputs
that? Why? If I change to const Complex& it behaves as expected. If
I remove operator double() it behaves as expected and calls the
Complex overload of operator <<.
the real part. I also define:
std:stream& operator<<(std:stream& os, Complex& complex)
{
os << "(" << complex.real<< ", " << complex.imaginary << ")";
return os;
}
Now if I write:
Complex a(1.0, 2.0)
Complex b(1.0, 2.0)
cout << a + b << endl;
I have overloaded operator+ for Complex.
However, when I write this, it calls operator double() and outputs
that? Why? If I change to const Complex& it behaves as expected. If
I remove operator double() it behaves as expected and calls the
Complex overload of operator <<.