How do I: string class operator+

Nov 6, 2011 at 9:42am
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream.h>
#include<string.h>
class myString{
	char *pStr;
public:
	myString():pStr(""){}
	myString(char *ch){
		this->pStr=ch;
	}
	myString operator+(myString Str){//maybe there is something wrong here 
		myString temp(this->pStr);
		strcat(temp.pStr,Str.pStr);
		return temp;
	}
	friend ostream& operator<<(ostream &out, myString &Str){
		out<<Str.pStr;
		return out;
	}
};

void main(){
	myString win("Windows"), xp("XP");
	win+xp;
	cout<<win; //this should display string "Windows" only.
}

Program output:
WindowsXP

The logical output is:
Windows

So what is wrong actually? is it because of using the pointer and reference or else? Any idea?
sorry for my bad english
Last edited on Nov 6, 2011 at 9:44am
Nov 6, 2011 at 11:43am
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.
Nov 6, 2011 at 12:22pm
First, add a

cout<<win;

before

win+xp;

so you can see if it really is win+xp that's doing it.
Last edited on Nov 6, 2011 at 12:29pm
Nov 6, 2011 at 2:08pm
In your code and your example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
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.
Nov 28, 2011 at 2:44pm
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..
Nov 28, 2011 at 3:00pm
1
2
3
4
5
6
7
myString operator+(myString Str)
	{//maybe there is something wrong here 
		myString temp;
		strcat(pStr ,Str.pStr);
		temp.pStr = pStr; 
		return temp;
	}


Also you need to overload = operator
Nov 29, 2011 at 5:35am
okay, i get it.. thanks
Nov 29, 2011 at 6:43pm
If it is not to late, you should make the argument to your operator+ method a constant reference.

myString operator+(myString const &Str)

This way you are not calling the copy constructor for myString when passing as an argument.
Topic archived. No new replies allowed.