| | |
Operator Overloading Help
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2009
Posts: 136
Reputation:
Solved Threads: 0
Been working with operator overloading for the first time and am running into quite the standstill with this one. I finally got rid of the code I had written and decided to start from scratch. If anyone has any advice on how to code this for me it be a great help for me to see an example on how this would work.
This is what I need to do for it.
MyString MyString::operator+(const MyString& rightOp) const
The addition operator should be overloaded to take two MyStrings, concatenate their text together, and return a new MyString that contains the result.
Here is what I originally had if this helps anyone solve my issue.
This is what I need to do for it.
MyString MyString::operator+(const MyString& rightOp) const
The addition operator should be overloaded to take two MyStrings, concatenate their text together, and return a new MyString that contains the result.
Here is what I originally had if this helps anyone solve my issue.
C++ Syntax (Toggle Plain Text)
MyString MyString::operator+(const MyString& rightOp) const { strcat(this->stringStorage, rightOp.stringStorage); return *this; }
Last edited by lancevo3; Jul 12th, 2009 at 3:02 pm.
I see two things wrong:
1) remove the const keyword at the end -- the function is not const because it changes the value of stringStorage.
2)You need another class object. What you wrote was for the += operator, not the + operator.
1) remove the const keyword at the end -- the function is not const because it changes the value of stringStorage.
2)You need another class object. What you wrote was for the += operator, not the + operator.
C++ Syntax (Toggle Plain Text)
MyString MyString::operator+(const MyString& rightOp) { MyString tmp = *this; // requires overloaded = operator strcpy(tmp.stringStorage, rightOp.stringStorage); return tmp; }
Last edited by Ancient Dragon; Jul 12th, 2009 at 3:21 pm.
•
•
•
•
The addition operator should be overloaded to take two MyStrings, concatenate their text together, and return a new MyString that contains the result.
c++ Syntax (Toggle Plain Text)
class MyString { // ... }; MyString operator+ (const MyString &lhs, const MyString &rhs) { return MyString(lhs) + MyString(rhs); }
"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
•
•
Join Date: Feb 2009
Posts: 136
Reputation:
Solved Threads: 0
I went ahead and applied the changes but I left const on the end because of the requirement would that throw things off?
C++ Syntax (Toggle Plain Text)
MyString MyString::operator+(const MyString& rightOp) const { MyString tmp = *this; strcpy(tmp.stringStorage, rightOp.stringStorage); return tmp; }
•
•
•
•
Don't you want this function to be outside of the class?
Or something like that?c++ Syntax (Toggle Plain Text)
class MyString { // ... }; MyString operator+ (const MyString &lhs, const MyString &rhs) { return MyString(lhs) + MyString(rhs); }
•
•
•
•
error C2270: '+' : modifiers not allowed on nonmember functions
Last edited by Ancient Dragon; Jul 12th, 2009 at 3:39 pm.
•
•
Join Date: Feb 2009
Posts: 136
Reputation:
Solved Threads: 0
Alright well have another question about a different + overloading because I have to do three and they are giving issues.
#
MyString MyString::operator+(const char* rightOp) const
The addition operator should be overloaded to take a MyString object and a C-style string, concatenate their text together, and return a new MyString object that contains the result.
Anymore advice would be great. I have to write three + overloads and they are just giving me an issue where the other ones I did not have as much problem.
C++ Syntax (Toggle Plain Text)
MyString MyString::operator+(const char* rightOp) const { strcat(this->stringStorage, rightOp); return *this; }
#
MyString MyString::operator+(const char* rightOp) const
The addition operator should be overloaded to take a MyString object and a C-style string, concatenate their text together, and return a new MyString object that contains the result.
Anymore advice would be great. I have to write three + overloads and they are just giving me an issue where the other ones I did not have as much problem.
Last edited by lancevo3; Jul 12th, 2009 at 3:40 pm.
Look at the function I posted above. All you have to do is change the parameter types in the three overloads
C++ Syntax (Toggle Plain Text)
MyString MyString::operator+(const MyString& rightOp) const MyString MyString::operator+(const char* rightOp) const MyString MyString::operator+(const std::string& rightOp) const
•
•
Join Date: Jul 2005
Posts: 1,706
Reputation:
Solved Threads: 274
char *strcat( char *str1, const char *str2 );
That's the prototype for strcat() from the online reference I usually refer to. I always thought str1 needed to be a null terminated char array and str2 needed to be either a null terminated char array or a string literal. I am also almost certain that the return value of char* will be a null terminated char array. From your previous post I thought that one of the requirements was that the MyString member varialbe called stringStorage could not be null terminated. If that restriction still holds, then you will have to be careful regarding using strcat() and other C style string handling functions to meet the assigment requirements.
That's the prototype for strcat() from the online reference I usually refer to. I always thought str1 needed to be a null terminated char array and str2 needed to be either a null terminated char array or a string literal. I am also almost certain that the return value of char* will be a null terminated char array. From your previous post I thought that one of the requirements was that the MyString member varialbe called stringStorage could not be null terminated. If that restriction still holds, then you will have to be careful regarding using strcat() and other C style string handling functions to meet the assigment requirements.
Klatu Barada Nikto
![]() |
Similar Threads
- a small prob with operator overloading (C++)
- Operator Overloading HELP! (C++)
- about operator overloading (C++)
- operator overloading (Java)
- Operator Overloading Question (C++)
- C++ Tic Tac Toe using classes & operator overloading (C++)
- program for finding factorial of a number using *operator overloading (C++)
Other Threads in the C++ Forum
- Previous Thread: OOP
- Next Thread: Newbie Question
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






