Thursday, July 9, 2009

Overloading the + operator for use with C-strings in C++?

I would appreciate any relevant samples of code...

Overloading the + operator for use with C-strings in C++?
#include %26lt;iostream.h%26gt;


#include %26lt;string.h%26gt;





struct String


{


public:


char * Data;


int Length;


};


String operator+(const String %26amp; s1,const String %26amp; s2)


{


char * s3=new char [s1.Length+s2.Length];


strcpy(s3,s1.Data);


strcat(s3,s2.Data);





String Out;


Out.Data=s3;


Out.Length=s1.Length+s2.Length;


return Out;


}





//you can't directly overload char * because at least one operand of any overloaded operator must be of some user-defined type.





//u can use strcat
Reply:no idea


No comments:

Post a Comment