Can someone explain the purpose of std::ostream &out for this friend overloaded operator function? why not just use std::cout inside the function?

  c++, ostream, syntax

Can someone explain the purpose of std::ostream &out for this friend overloaded operator function? why not just use std::cout inside the function?

friend std::ostream & operator << (std::ostream &out, const Complex &c) 
{ 
    out << c.real; 
    out << "+i" << c.imag << endl; 
    return out; 
}

like so:

friend operator << (const Complex &c) 
{ 
    std::cout << c.real; 
    std::cout << "+i" << c.imag << endl; 
}

Source: Windows Questions C++

LEAVE A COMMENT