Use overloaded assignment (=) operator with self-assignment - C++ STL

C++ examples for STL:string

Description

Use overloaded assignment (=) operator with self-assignment

Demo Code

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

int main() /*from  www.j a  v  a  2  s. c  o m*/
{ 
    string s1( "happy" ); 
    string s2( " birthday" ); 
    string s3; 

   // Use string copy constructor 
   string s4( s1 ); 

   
   cout << "assigning s4 to s4" << endl; 
   s4 = s4; 
   cout << "s4 = " << s4 << endl; 

}

Result


Related Tutorials