String & String::operator +(String &str) {
...
String * strPointer = new String();
...
String &result = *strPointer;
...
return result;
}
You manually created the object, and then returned the reference to it. Hence, the compiler did not insert delete
operator. So, you cannot return reference without memory leak and manually destroing the object.
For example in Qt
library operator+
implemented as
Q_EXPORT inline const QString operator+( const QString &s1, const QString &s2 ) {
QString tmp( s1 );
tmp += s2;
return tmp;
}
Where operator+=
declared as QString &operator+=( const QString &str );