Here is one example: When you overload the << operator for class T , the signature will be:
std::ostream operator<<(std::ostream& os, T& objT )
where the implementation should be
{
For the << operator, the first argument must be an ostream object, and the second should be an object of class T.
If you try to define operator<< as a member function, you will not be allowed to define it as std::ostream operator<<(std::ostream& os, T& objT) . This is because member functions of a binary operator can take only one argument, and the caller is implicitly passed as the first argument using this .
If you use the signature std::ostream operator<<(std::ostream& os) as a member function, you will actually get the member function std::ostream operator<<(this, std::ostream& os) , which will not do what you want. Therefore, you need an operator that is not a member function and can access member data (if your class T has the personal data you want to pass, operator<< must be a friend of class T).
ali Dec 15 '13 at 12:37 2013-12-15 12:37
source share