I have some more basic questions about using overloaded operators. This is just a hypothetical question.

Say I have 2 overloaded operators functions defined for a class. The overloaded operator= function and the overloaded+ function.

Now I want to create the overloaded operator+=. Can I define that function in terms of the other overloaded operator= function and overloaded operator+ function ? Meaning can I use them in the definition of the overloaded operator+=? Or do I have to write a whole new function essentially doing what both the overloaded operator+ and the overloaded operator= functions do but all in one function? I just want to do this the right way but really don't know the preferred way to do it.

I know I can write the code to make the overloaded operator+= to work without using the other overloaded functions but it seems overly complex and I have been reading a lot of so called good design practices lately and it clearly seems the general consensus is that complexity is a cost don't pay it 2x and KISS. So with that being said it seems that I should keep a single point of failure for the new function relying on the previously defined functions so to not have to re-write 3 functions in case I need them to do something else.
This is just a simple string class that has overloaded operators for concatenating strings and copying strings.

class CSimpleString

	// Constructor definition.
	CSimpleString(const char* text = "Default Text")
		pmessage = new char [strlen(text) + 1];
		strcpy_s(pmessage, strlen(text) + 1, text);
		m_Length = strlen(pmessage);
	// Overloaded assignment operator.
	CSimpleString& operator=(const CSimpleString& aSimpleString)
	{ /*Do some stuff*/}
	// Overloaded addition operator.
	CSimpleString operator+(const CSimpleString& aSimpleString) const
	{/*Do some more stuff*/}
         // Overloaded += operator.
        CSimpleString operator+=(const CSimpleString& aSimpleString)
        {/*What would be the syntax for using the overloaded operator+ and overloaded operator= in this funcntion? */}

char* pmessage;
size_t m_Length;

What would be the syntax for using the overloaded operators inside another overloaded operator? Would I have to use them specifically as the function is defined? Like so...

// Overloaded += operator.
        CSimpleString operator+=(const CSimpleString& aSimpleString)
        {  string1 operator=(string1 operator+(string2)) }

Or is this completely stupid to do it this way? And I need a whole new function? I'm just trying to go about this correct and preferred way with most recycling of code.

Thank You for any help and wisdom on this matter.

8 Years
Discussion Span
Last Post by Vallnerik25

Look, you are on the right path.
If you have already overloaded the operator= and operator+, you can use them in the definition of operator+= .
Say the parameter of the += operator is aSimpleString.Then, the definition of += operator may look like.

Votes + Comments
He helped me solve my question quickly and with correct and kind suggestions. Awesome poster!

That's what I'm doing but I get a
error C2064: term does not evaluate to a function taking 1 arguments.

But now that I know I'm on the right path I know I'm going to figure it out.

Thx Siddhant3s, I love these forums. I get quick and correct answers to my questions.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.