how do you know when to use operator overloading?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2009
Posts: 265
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

how do you know when to use operator overloading?

 
0
  #1
Aug 7th, 2009
I understand the concept but i don't know when to use and what my teacher expects when he says overload certain operators.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #2
Aug 7th, 2009
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

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

 
0
  #3
Aug 7th, 2009
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 ~.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,364
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #4
Aug 7th, 2009
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 265
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

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

 
0
  #5
Aug 7th, 2009
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()???
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

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

 
0
  #6
Aug 7th, 2009
Well try it
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 265
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

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

 
0
  #7
Aug 7th, 2009
i have tried it just not sure why he wants me to overload the >> operator.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 265
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

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

 
0
  #8
Aug 7th, 2009
so basically here i want the buffer to continue to read until it hits the \n character?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC