943,902 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 582
  • C++ RSS
Aug 7th, 2009
0

how do you know when to use operator overloading?

Expand Post »
I understand the concept but i don't know when to use and what my teacher expects when he says overload certain operators.
Similar Threads
Reputation Points: 47
Solved Threads: 2
Posting Pro in Training
lotrsimp12345 is offline Offline
413 posts
since Jun 2009
Aug 7th, 2009
0

Re: how do you know when to use operator overloading?

Normally overload operators so that you can do mathematical or other operations with c++ classes. for example: cout << MyClass; assuming MyClass is an instance of some c++ class and you want that class to print the value of its class variables to the screen. Or you might want to add them MyClass++; There are lots of things you can do with overloaded operators, those are just two of them.

Which operators to overload depends on the c++ class and what you want it to do.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Aug 7th, 2009
0

Re: how do you know when to use operator overloading?

You overload an operator when it makes sense to do so.

Say that you create a custom string class MyString.

It makes sense to overload the + operator, but not the ~.
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Aug 7th, 2009
0

Re: how do you know when to use operator overloading?

Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 7th, 2009
0

Re: how do you know when to use operator overloading?

i have a project which i am working on. I am supposed to read in a phrase then every time it encounters a space the word is supposed to go to a newline in my output. The phrase can take up only one line. The phrase can be read from a file or from cin. The phrase contains spaces so i am guessing you are supposed to overload input >> to make it take a space containing line when they enter cin>>string word. by overloading with getline()???
Reputation Points: 47
Solved Threads: 2
Posting Pro in Training
lotrsimp12345 is offline Offline
413 posts
since Jun 2009
Aug 7th, 2009
0

Re: how do you know when to use operator overloading?

Well try it
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Aug 7th, 2009
0

Re: how do you know when to use operator overloading?

i have tried it just not sure why he wants me to overload the >> operator.
Reputation Points: 47
Solved Threads: 2
Posting Pro in Training
lotrsimp12345 is offline Offline
413 posts
since Jun 2009
Aug 7th, 2009
0

Re: how do you know when to use operator overloading?

so basically here i want the buffer to continue to read until it hits the \n character?
Reputation Points: 47
Solved Threads: 2
Posting Pro in Training
lotrsimp12345 is offline Offline
413 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Enquiries about fstream
Next Thread in C++ Forum Timeline: Processing Numbers from a file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC