As we know that operation like int i=1, j=2; i+j; will not change the "i" or "j" variables' value because it only change for a temporary when the statement is performed. But in my case, the object of a class change its value when I execute the operator+ statement.
Why are you using such old header files? The use of header files like iostream.h and string.h is deprecated in c++. What compiler do you use anyway?
Does your code even compile? When I try it with some modifications it throws a SEGFAULT. I spot it throws it at line win+xp; which means you probably have some boundary break here. In your function you don't expect to change any member of your class use const. That way your compiler will complain when somewhere it it changed: const myString operator+(myString Str) const
The first const refers to the return value the second to the data member of your class.
myString operator+(myString Str){
myString temp(this->pStr); // this will pass the pointer of array into temp, in the
// temp, the pStr points to "XP"
strcat(temp.pStr,Str.pStr); // the pStr in temp points to "WindowsXP"
return temp; // this is a shallow copy of temp, which only copy the
// pointer itself, not the pointee. So it returns a pointer
// to "WindowsXP"
// since Str and temp share the same pStr pointer value,
// pointer in Str is modified as well.
}
To prevent such case, i think the best way is to allocate memory for your string in your class myString and perform deep copy instead of shallow copy.
i have tried, but to no avail. i'm using the turbo c++ 4.5, but it is better if you show me the example code for the newest c++ compiler, i have microsoft visual c++ 2010..