I am learning c++ on my own. I was studying operator overloading, i was able to understand addition and subtraction operator overloading. But overloading of I/O operators is a bit confusing. I have created a class for Complex numbers, now i am overloading operators.
Function prototype from Complex.h
friend ostream& operator<<(ostream&, const Complex&);
Function from Complex.cpp
ostream& operator<<(ostream& os, const Complex& value){
os << "(" << value.r <<", "
<< value.i << ")" ;
return os;
}
- Can anyone explain (on a basic level) why we have to use a friend function declaration here?
- Why do we have to pass all arguments and the return type of the operator by reference?
- This function works fine without using const, but why are we using const here? What is the advantage of passing Complex as a constant reference?
operator<<
andComplex::operator/
... Copy/Paste error? If not, specify the problem on each one. – Holt