Solved

Operator Overloading

Posted on 1998-04-18
10
208 Views
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
0
Comment
Question by:admiral
[X]
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
10 Comments
 
LVL 22

Accepted Solution

by:
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.
0
 
LVL 22

Expert Comment

by:nietod
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1161919
By "it has to do with hoiw they are called"  I mean the right answer has to do...
0
Technology Partners: 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

by:alexo
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.
OK?
0
 
LVL 22

Expert Comment

by:nietod
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.)
0
 
LVL 11

Expert Comment

by:alexo
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.
0
 
LVL 22

Expert Comment

by:nietod
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.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1161924
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).
0
 
LVL 22

Expert Comment

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

Author Comment

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

0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

695 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