473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Overloading operator "<<"

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

Mar 25 '07 #1
1 1736
Sunny wrote:
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);

--
Robert Bauck Hamar
Semikolon markerer en litt kortere pause enn punktum; det kan bare
stå mellom selvstendige setninger. Det er en moderne uskikk å bruke
semikolon istedenfor kolon. Følg ikke den uskikken!
Mar 25 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Eric A. Forgy | last post by:
Hello, I am just learning Java and am trying to write a method that does something like //=========================================== public Static List find(double array,double val,String...
1
by: Christian Schmidbauer | last post by:
Hello! I prepare my XML document like this way: ------------------------------------------------------- PrintWriter writer; Document domDocument; Element domElement; // Root tag
7
by: Diandian Zhang | last post by:
Does anyone have an idea, how to do it? Thanks in advance!
6
by: Guagua | last post by:
I have lots of expression like "a <?= b" in my original code. However I switch to another compiler from gcc that doesn't support operator "<?=". How could I define the operator so that the...
2
by: andrew007 | last post by:
I do xml / xslt transformation using asp.net but I found any value (w/xml format) in xml node html-encoded to &lt and &gt format if it's > or < tag. Since I have sub xml data in a parent xml node...
12
by: Filipe Sousa | last post by:
Hi! Could someone explain to me why this operation is not what I was expecting? int main() { int x = 2; std::cout << x << " " << x++ << std::endl; return 0; }
3
by: key9 | last post by:
Hi all I am confuse of how to override operate "<<" Sample , I've got 2 class class Terminal { public:
8
by: Prateek | last post by:
Hey... Can anybody tell me why "<<" operator is used behind cout? Like: cout<<"Welcome"; My one friend told me that it "shifts the bits"...But it's not very clear to me...Can anybody tell?
1
by: bravephantom | last post by:
can i overload the operator ">" and "<" with their same names to be used for objects? if its possible plz explain how thnx for help
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.