954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Operator Overloading Help

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.

MyString MyString::operator+(const MyString& rightOp) const
	{
	strcat(this->stringStorage, rightOp.stringStorage);
	return *this;
	}
lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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.

MyString MyString::operator+(const MyString& rightOp)
{
    MyString tmp = *this; // requires overloaded = operator
    strcpy(tmp.stringStorage, rightOp.stringStorage);
    return tmp;
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I don't have a choice on the const keyword on the end because its part of the assignment. Should I be returning rightOp then?

lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 
The addition operator should be overloaded to take two MyStrings, concatenate their text together, and return a new MyString that contains the result.


Don't you want this function to be outside of the class?

class MyString
{
    // ...
};

MyString operator+ (const MyString &lhs, const MyString &rhs)
{
    return MyString(lhs) + MyString(rhs);
}

Or something like that?

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

I went ahead and applied the changes but I left const on the end because of the requirement would that throw things off?

MyString MyString::operator+(const MyString& rightOp) const
        {
        MyString tmp = *this;
        strcpy(tmp.stringStorage, rightOp.stringStorage);
        return tmp;
        }
lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Yup -- I tested that too and it compiled ok.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Don't you want this function to be outside of the class?

class MyString
{
    // ...
};

MyString operator+ (const MyString &lhs, const MyString &rhs)
{
    return MyString(lhs) + MyString(rhs);
}

Or something like that?

No -- that is illegal syntax for non-class methods. error C2270: '+' : modifiers not allowed on nonmember functions

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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
        {
        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.

lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Look at the function I posted above. All you have to do is change the parameter types in the three overloads

MyString MyString::operator+(const MyString& rightOp) const
MyString MyString::operator+(const char* rightOp) const
MyString MyString::operator+(const std::string& rightOp) const
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

I was able to finish the assignment today he showed me how the plus overload was suppose to work which made the other two alot easier to code, thank you very much for the help.

lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You