DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   how do you know when to use operator overloading? (http://www.daniweb.com/forums/thread209737.html)

lotrsimp12345 Aug 7th, 2009 12:30 am
how do you know when to use operator overloading?
 
I understand the concept but i don't know when to use and what my teacher expects when he says overload certain operators.

Ancient Dragon Aug 7th, 2009 12:37 am
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.

minas1 Aug 7th, 2009 12:39 am
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 ~.

Dave Sinkula Aug 7th, 2009 12:42 am
Re: how do you know when to use operator overloading?
 
http://www.parashift.com/c++-faq-lit...erloading.html

lotrsimp12345 Aug 7th, 2009 12:44 am
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()???

minas1 Aug 7th, 2009 12:47 am
Re: how do you know when to use operator overloading?
 
Well try it :)

lotrsimp12345 Aug 7th, 2009 12:48 am
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.

lotrsimp12345 Aug 7th, 2009 1:01 am
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?


All times are GMT -4. The time now is 7:05 am.

Forum system based on vBulletin Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
©2003 - 2010 DaniWeb® LLC