Overloading operator "<<"

S

Sunny

The way to overload operator << is : ostream& operator << (ostream&
os, const Obj& obj);
and this is a member function.
My question is why do we need to provide a const reference of Obj as
argument when it is a member function and its members accessible
through this pointer ?
Isnt cout << obj;
equivalent to obj.operator<<(cout) ?
Thanks
 
R

Robert Bauck Hamar

Sunny said:
The way to overload operator << is : ostream& operator << (ostream&
os, const Obj& obj);
Yes

and this is a member function.
No

My question is why do we need to provide a const reference of Obj as
argument when it is a member function and its members accessible
through this pointer ?

It's not a member of Obj.
Isnt cout << obj;
equivalent to obj.operator<<(cout) ?

No, it is equivalent to either of

cout.operator<<(obj);
operator<<(cout, obj);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top