Consider the following code snippet:
class Complex {
double real, im;
public:
Complex(double real, double im) {
this->real = real;
this->im = im;
}
Complex operator + (Complex const& sec) {
Complex ret(real + sec.real, im + sec.im);
return ret;
}
friend ostream& operator<<(ostream& os, const Complex& com);
};
ostream& operator << (ostream& os, const Complex& com) {
os << com.real << " " << com.im << "i";
return os;
}
Why doesn't the operator overloading of +
have to be declared as friend
as it will also want to access the private variables of the class from outside, similar to when overloading the <<
operator? At least that's what I thought but apparently this is not the case.