Operator Overloading

Posted on 1998-04-18
Last Modified: 2010-04-10
Hello, I am a CS student at the University of Florida
A Professor has asked " Why is it better to use a friend instead of member
function to overload the << operator.

My response: A formatting issue ,the friend function allows the function to
adapt to the data type needed to be output,

I am unclear on the technical issues ? any Insight
Question by:admiral
  • 6
  • 3
LVL 22

Accepted Solution

nietod earned 100 total points
ID: 1161917
No.  A friend function or a member function could have the same abilities.  Which is what you are talking about.  It has to do with how they are called.  I can't (ethically) tell you anymore.  But if you write a function both ways and try it both ways you will SEE the difference.
LVL 22

Expert Comment

ID: 1161918
Whoops.  That was supposed to be a comment.  Not an answer.  I don't think the question (or my response) is deserving of an answer.  Try it and I think you will see the answer.
LVL 22

Expert Comment

ID: 1161919
By "it has to do with hoiw they are called"  I mean the right answer has to do...
LVL 11

Expert Comment

ID: 1161920
Since the question is locked, this will have to go as a comment.

The << operator is a function, actually called operator<<().
Now, consider you want to overload it on class X.

Since the first operand is an ostream, if you want it to be a member function - it must be a member of ostream.  However, ostream is a library class.  You are not guaranteed access to the source, so you cannot change it.

Another alternative is deriving from ostream.  However, if you put class X in a library of its own - you'll have to supply your derived class along with it and make sure it is used.  Imagine the mess when using multiple libraries implemented that way...

So, you make operator<<(ostream, X&) a friend and avoid all those problems.
LVL 22

Expert Comment

ID: 1161921
Since Alexo is "giving stuff away" and since his comments will seem to contradict mine (they don't really contradict, we we're looking at things differently).  Let me explain what I was getting at.  If you overload operator >>  as a member of a class, then the class has to go on the left of the operator.  Like

SomeObject >> cin;

That is backwards of normal and it just plain looks missleading.  It looks like the information is going from the object to cin, rather than the reverse.  The usuall way it is written is

cin >> SomeObject;

But now the object of your class is on the right.  Thus overloading operator >> for the class you've written won't help in this format.

Alexo looked at the same problen differently.  He said well the stream (cin) has to be on the left.  That means if the operator >> is going to be overloaded we need to change the stream class.  That is a bad idea.  

So you see we don't really contradict each other.  I hope this helps. (But not too much, because you should try to work through this on your own.)
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

LVL 11

Expert Comment

ID: 1161922
I am "giving stuff away" because I find comments like "<expert name> is wrong!  Reject his answer and I'll tell you the correct one." distasteful.  I will provide the information that I think is corrent and it is up to the asker to determine who gets the points.

There is no official code of conduct between "experts" so I'm sticking to my personal one.
LVL 22

Expert Comment

ID: 1161923
Alexo, Sorry, that was not a personal attack.  (At least it was not meant to be.)  But it did seem to me like you gave a bit too much information to give to a student.  (I'm an ex teacher of sorts--one the students probably hated)  Yes, there is no code of conduct between experts--and we've both seen it get pretty bad (and I do not want that to happen between us)--but there is a code of conduct for students--the code of academic hononesty.  The code changes from institution to institution, but at a minimum it would never allow a student to seek an entire answer from a single outside source.  If I had assigned the question, I would feel the student had not gotten the experience I would have intended by getting an answer in this way.  Obviously you see it differently.  From our other interactions I know you would not intentionally do something dishonest.  I'm just suggesting that perhaps you did not see the extent to which you answered the question.  Or perhaps you did see the extent, but did not feel it was too much as, admititly there is a gray area here.
LVL 11

Expert Comment

ID: 1161924

>> Sorry, that was not a personal attack
I know.  I didn't treat it as one.  I also misunderstood your "giving away" remark because of the reasons stated in the following paragraph.

>> But it did seem to me like you gave a bit too much information to give to a student.
Maybe.  I share your views on academic honesty but I understood the phrase "A Professor has asked" to mean a question asked in class, not homework assignment.  This could be caused from the nature of the lectures in the institutions I attneded and from my personal teaching style or from my imperfect command of English (my third language).

>> and I do not want that to happen between us
I see no reason for it to happen.  Have you seen "The princess bride"?  There is a phrase Wesley says to Inigo montoya (just before he knocks him out) which I find appropriate.  I'll leave finding the exact reference to you :-)

>> I'm just suggesting that perhaps you did not see the extent to which you answered the question
On the contrary.  I try to give concise but complete answers (if I can).  I was not aware however that I was doing someboy else's homework.

If you have any future issues with my answers or comments, don't hesitate to email me (and don't try too hard to be politically correct, I'm not *that* sensitive).
LVL 22

Expert Comment

ID: 1161925
Good.  Perhaps I overeacted--not you.  Sleep well and dream of large women.

Author Comment

ID: 1161926
alexo interpreted the question as it was meant insofar as scope, this was not home work or anything, just conjecture


Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now