![]() |
| ||
| 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. |
| ||
| 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. |
| ||
| 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 ~. |
| ||
| Re: how do you know when to use operator overloading? |
| ||
| 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()??? |
| ||
| Re: how do you know when to use operator overloading? Well try it :) |
| ||
| 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. |
| ||
| 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