I am updating cpp after a long space, trying to understand operator overload methods. I tried to overload operator <to output the elements of the object. But I cannot do this without using a friend function. I am looking for a method without using a friend function.
here is my def class:
class Add{ private: int x; public: friend ostream& operator<<(ostream& ostr, Add const& rhs); //Method 1 void operator<<(ostream& ostr); //Method 2 };
implementation of functions
//Method 1 ostream& operator<<(ostream &ostr, Add const& rhs) { ostr<<rhs.x; return ostr; } //Method 2 void Add::operator<<(ostream& ostr) { cout<<" using operator<< \n"; ostr<<x; }
calls the main function
cout<<Obj_Add; //calls the Method 1 Obj_Add<<cout; //calls the Method 2
Now my question is: I would like to receive type 1 calls to the method without using the friend function. But I donβt know if this is possible or not in cpp. I tried several implementation options, but they all give me compilation errors. Please help me understand that I am not here.
source share