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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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...
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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

>> 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.
admiralAuthor Commented:
alexo interpreted the question as it was meant insofar as scope, this was not home work or anything, just conjecture

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.