On running the gdb session I found that t (of mystring variable class) is destructed by after the operator +() function (before returning back to main) and therfore no value s1 is being printed. I can not understand why for t variable destructor is called even though it has global scope.
Please suggest the corrective solution for the same.
what u suggested worked perfectly fine but can u plz tel me why are we passing arguments by reference to operator+ and operator=,
why following statements didn't work:
operator+(mystring a,mystring b); why should we use const...??
operator+(mystring a,mystring b); should also work, it good practice to pass parameter as const reference if we are not modifying its value in side the function.
it good practice to pass parameter as const reference if we are not modifying its value in side the function.
For non-native types. Passing int, double, etc by const ref is a bit pointless.
A ref is like a pointer, in that you are passing the address of the variable. So the trade off is between the cost of copying the data (if you pass by value) against a pointer deref (if you pass by ref).
For 32 bit systems, the size of a pointer is the same as an int. So it is actually a little bit slower to pass an int by ref, as you are copying exactly the same ammount of data (a 32-bit int or a 32-bit pointer to the same int).
While I usually pass std::string by const ref, really tiny structs (like a 2D point class, which is the size of 2 ints) I generally pass by value. As I do long long/__int64 parameters.