Link to home
Start Free TrialLog in
Avatar of admiral
admiral

asked on

Operator Overloading

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
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

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.
By "it has to do with hoiw they are called"  I mean the right answer has to do...
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.
OK?
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.)
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.
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.
Todd,

>> 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).
Good.  Perhaps I overeacted--not you.  Sleep well and dream of large women.
Avatar of admiral

ASKER

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