Use overloaded += : overload assignment operator « Operator Overloading « C++ Tutorial






#include <iostream>
#include <string>
using namespace std;

int main()
{
   string s1( "cat" ), s2, s3;

   s2 = s1;          
   s3.assign( s1 );  
   cout << "s1: " << s1 << "\ns2: " << s2 << "\ns3: "
        << s3 << "\n\n";

   // overloaded +=
   s3 += "pet";             // create "carpet"

   return 0;
}








10.3.overload assignment operator
10.3.1.Overloading the copy assignment operator
10.3.2.Define -, + and = for the ThreeD class
10.3.3.Overload operator plus (=)
10.3.4.Overload assignment for Point
10.3.5.Use overloaded +=