Problem with friend function
dear everybody ..
I'm trying to understand operator overloading with my class (Complex) that handles complex numbers .. I'd like to allow such an operation
Code:
Complex c1(2,-3); //c1=2-3i
Complex c2(0,0); //c2=0;
c2=3+c1; //c2 should equal 5-3i
c1 will not call the operator overloading function, instead, the compiler will call it with two parameters: 4 and c1 .. so the overloading function should be defined as standalone function outside Complex class and should look like this:
Code:
Complex operator +(float r, Complex c)
{
return Complex(r+c.getReal(),c.getImag());
}
My problem is:
When the overloading function is defined as above, it doesn't work probably, but if it has been defined as a friend function inside the class, it's perfect.
I'm confused because even I don't use friend function, I'm still using getters to access private members like real and imag, so my understanding guides me that there is no need for friend function !!
Can anybody help ?