Operator Overloading

Posted on 1998-04-18
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
LVL 22

Accepted Solution

nietod earned 400 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...
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.)
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

801 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