Overloading &

I am having an issue with overloading the & operator. The goal is to concat 2 string objects and return the concatenated string without changing either object. The program builds, but fails when it is run. I have removed a couple of non-relevant functions to stay within the size limit for this post. I have already turned in the assignment, but this is bugging me. Can anyone see why this wouldn't work? Thanks in advance.

-----String.h--------
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#ifndef STRING_H
#define STRING_H

#include <cstring>
#include <iostream>
#include <assert.h>

using namespace std;

class String
{
  public:
		     String		();				
		     String		(const String &);
    explicit	     String		(const char []);
		     ~String		();

          int	     Compare		(const String &)	const;
	  int	     Compare		(const char [])		const;
	  String &   Concat		(const String &);
	  String &   Concat		(const char []);
	  String &   Copy		(const String &);
	  String &   Copy		(const char []);
	  void	     Display		(ostream &)             const;
	  size_t     Length		()                      const;
  const	  String &   operator =	        (const String &); 
  const	  String &   operator =	        (const char []);
	  bool	     operator <	        (const String &)	const;
	  bool	     operator <	        (const char [])		const;
	  bool	     operator >	        (const String &)	const;
	  bool	     operator >	        (const char [])		const;
	  bool	     operator <=	(const String &)	const;
	  bool	     operator <=	(const char [])		const;
	  bool	     operator >=	(const String &)	const;
	  bool	     operator >=	(const char [])		const;
	  bool	     operator ==	(const String &)	const;
	  bool	     operator ==	(const char [])		const;
	  bool	     operator !=	(const String &)	const;
	  bool	     operator !=	(const char [])		const;
	  String &   operator &	        (const String &);
	  String &   operator &	        (const char []);
	  String &   operator &=	(const String &);
	  String &   operator &=	(const char []);
   friend ostream &  operator <<        (ostream &, const String &);
   friend istream &  operator >>        (istream &, const String &);
				
private:
	  char *     pChar;
	  char *     pCharTmp;
	  size_t     NumChars;
	  size_t     MaxChars;	

};

inline int String::Compare(const String & S) const
{
return strcmp(pChar, S.pChar);
}

inline int String::Compare(const char Char []) const
{
return strcmp(pChar, Char);
}

inline void String::Display (ostream & out) const
{
out << pChar;
}

inline size_t String::Length () const
{
return NumChars;
}

inline bool operator < (const char C [], const String & S)
{
return (S > C);
}

inline bool operator > (const char C [], const String & S)
{
return (S < C);
}

inline bool operator <= (const char C [], const String & S)
{
return (S >= C);
}

inline bool operator >= (const char C [], const String & S)
{
return (S <= C);
}

inline ostream & operator <<(ostream & out, const String & S)
{
out << S.pChar;
return out;
}

inline istream & operator >>(istream & in, const String & S)
{
in >> S.pChar;
return in;
}	

#endif 


-----String.cpp-----
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "String.h"
#include <string.h>

#pragma warning (disable:4996)
#pragma warning (disable:4172)

String::String()
{
NumChars = 0;
MaxChars = 0;
pChar = new char [1];
pChar [0] = '\0';
}

String::String(const String & S)
{
NumChars = S.NumChars;
MaxChars = S.NumChars;
pChar = new char [S.NumChars + 1];
strcpy(pChar,S.pChar);
}

String::String(const char Chars [])
{
NumChars = strlen(Chars);
MaxChars = NumChars;
pChar = new char [NumChars + 1];
strcpy(pChar, Chars);
}

String::~String()
{
delete [] pChar;
}

String & String::Concat(const String & S)
	{
	if (MaxChars < (NumChars + S.NumChars))
		{
		pCharTmp = new char [NumChars + S.NumChars + 1];
		strcpy(pCharTmp,pChar);
		delete [] pChar;
		pChar = new char [NumChars + S.NumChars + 1];
		MaxChars = (NumChars + S.NumChars);
		strcpy(pChar, pCharTmp);
		}
	else;
		strcat(pChar,S.pChar);
		NumChars = MaxChars;
		return *this;	
	}

String & String::Concat(const char C [])
	{
	if (MaxChars < (NumChars + strlen(C)))
		{
		pCharTmp = new char [NumChars + strlen(C) + 1];
		strcpy(pCharTmp,pChar);
		delete [] pChar;
		pChar = new char [NumChars + strlen(C) + 1];
		MaxChars = NumChars;
		strcpy(pChar, pCharTmp);
		}
	else;
		strcat(pChar,C);
		NumChars = MaxChars;
		return *this;	
	}

String & String::Copy(const String & S)
	{
	NumChars = S.NumChars;
	if (MaxChars < S.NumChars)
		{
		delete [] pChar;
		MaxChars = NumChars;
		pChar = new char [NumChars + 1];
		}
		else;
	strcpy(pChar, S.pChar);
	return *this;
	}

String & String::Copy(const char Char [])
	{
	NumChars = strlen(Char);
	if (MaxChars < strlen(Char))
		{
		delete [] pChar;
		MaxChars = NumChars;
		pChar = new char [NumChars + 1];
		}
		else;
	strcpy(pChar,Char);
	return *this;
	}

const String & String::operator = (const String & S)
	{
	NumChars = S.NumChars;
	if (MaxChars < S.NumChars)
		{
		delete [] pChar;
		pChar = new char [NumChars + 1];
		}
		else;
	strcpy(pChar, S.pChar);
	return *this;
	}

const String & String::operator = (const char Char [])
	{
	NumChars = strlen(Char);
	if (MaxChars < strlen(Char))
		{
		delete [] pChar;
		MaxChars = NumChars;
		pChar = new char [NumChars + 1];
		}
		else;
	strcpy(pChar,Char);
	return *this;
	}

bool String::operator < (const String & S) const
	{
	if (strcmp(pChar,S.pChar) < 0)
		return true;
	else
		return false;
	}

bool String::operator <(const char Char []) const
	{
	if (strcmp(pChar,Char) < 0)
		return true;
	else
		return false;
	}

bool String::operator > (const String & S) const
	{
	if (strcmp(pChar,S.pChar) > 0)
		return true;
	else
		return false;
	}

bool String::operator >(const char Char []) const
	{
	if (strcmp(pChar,Char) > 0)
		return true;
	else
		return false;
	}

bool String::operator <= (const String & S) const
	{
	if (strcmp(pChar,S.pChar) <= 0)
		return true;
	else
		return false;
	}

bool String::operator <= (const char Char []) const
	{
	if (strcmp(pChar,Char) <= 0)
		return true;
	else
		return false;
	}

bool String::operator >= (const String & S) const
	{
	if (strcmp(pChar,S.pChar) >= 0)
		return true;
	else
		return false;
	}

bool String::operator >= (const char Char []) const
	{
	if (strcmp(pChar,Char) >= 0)
		return true;
	else
		return false;
	}

String & String::operator &= (const String & S)
	{
	if (MaxChars < (NumChars + S.NumChars))
		{
		pCharTmp = new char [NumChars + S.NumChars + 1];
		strcpy(pCharTmp,pChar);
		delete [] pChar;
		pChar = new char [NumChars + S.NumChars + 1];
		MaxChars = (NumChars + S.NumChars);
		strcpy(pChar, pCharTmp);
		}
	else;
		strcat(pChar,S.pChar);
		NumChars = MaxChars;
		return *this;	
	}
	
	
String & String::operator &= (const char C [])
	{
	if (MaxChars < (NumChars + strlen(C)))
		{
		pCharTmp = new char [NumChars + strlen(C) + 1];
		strcpy(pCharTmp,pChar);
		delete [] pChar;
		pChar = new char [NumChars + strlen(C) + 1];
		MaxChars = NumChars;
		strcpy(pChar, pCharTmp);
		}
	else;
		strcat(pChar,C);
		NumChars = MaxChars;
		return *this;	
	}

String & String::operator & (const String & S)
	{
	String StrTmp (*this);
	pCharTmp = new char [StrTmp.Length() + S.Length() + 1];
	strcpy(pCharTmp, StrTmp.pChar);
	strcat(pCharTmp, S.pChar);
	delete [] StrTmp.pChar;
	StrTmp.pChar = new char [strlen(pCharTmp) + 1];
	strcpy(StrTmp.pChar,pCharTmp);
	return StrTmp;
	}

String & String::operator & (const char C [])
	{
	String StrTmp (*this);
	pCharTmp = new char [StrTmp.Length() + strlen(C) + 1];
	strcpy(pCharTmp, StrTmp.pChar);
	strcat(pCharTmp, C);
	delete [] StrTmp.pChar;
	StrTmp.pChar = new char [strlen(pCharTmp) + 1];
	strcpy(StrTmp.pChar,pCharTmp);
	return StrTmp;
	}


-----Main.cpp-----
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string.h>
#include "String.h"

using namespace std;

void main()
	{	
	String A1 ("testing 123");
	String A2 ("testing 234");
	String A3 (A1);
	String A4 (A2);
	String A5;

	//Testing & builds, but fails to run.
	//A5 = (A1 & A2);
	//A5 = (A3 & A4);
	}

That is because you're returning a reference to a local variable. The object is destroyed when the function returns and a dangling reference is returned.
Notes:
1. the standard operator for string concatenation is +
2. void main() is incorrect. It must be int main().
3. Code in the form if (x)return true; else return false; can and should be written as return x;


I guess the problem is that you are returning a reference to an object that goes out of scope

224
225
226
227
228
229
230
231
232
233
234
String & String::operator & (const String & S)
	{
	String StrTmp (*this);
	pCharTmp = new char [StrTmp.Length() + S.Length() + 1];
	strcpy(pCharTmp, StrTmp.pChar);
	strcat(pCharTmp, S.pChar);
	delete [] StrTmp.pChar;
	StrTmp.pChar = new char [strlen(pCharTmp) + 1];
	strcpy(StrTmp.pChar,pCharTmp);
	return StrTmp; // returned the reference to StrTmp, but it goes out of scope
	}

Thanks guys! I guess I need to go back and do some reading about scope and dangling pointers. Your response makes sense though.

1. Using & for the operator was what the assignment asked for.
2. Our instructor always uses void main. Not sure why, but I understand it is considered bad form, but haven't researched why yet.
3. Very good point. I will revise the code.

So, any hints on how you would go about solving this? I am starting to lean towards using a non-member function as passing it two parameters. Is this how you would go about it?
So, any hints on how you would go about solving this? I am starting to lean towards using a non-member function as passing it two parameters. Is this how you would go about it?

No, you fix it by returning a String object instead of a String reference.

About 2.: The C++ standard dictates that main must return int. No other return types are allowed.

By the way, I see this a lot in your code: else;
Surely this isn't intentional?

Edit: one more note, operator& should be const.
Last edited on
Ohhh, it just clicked into place. I will re-write it and see how it goes.

About 2.: I will start using int main instead of void. I will also quiz the instructor as to why he uses void. Advice taken. Thanks.

I think that else; is another quirk I picked up from the instructor. I see no need for it so I won't use it.

Thanks about the operator & mention. I will change it to const.

I will make some changes and post back about the final solution.
OK,

So I changed it to use the previously written Conat function using the following and returned an object instead of a reference and all is well.

instead of:
1
2
3
4
5
6
7
8
9
10
11
String & String::operator & (const String & S)
	{
	String StrTmp (*this);
	pCharTmp = new char [StrTmp.Length() + S.Length() + 1];
	strcpy(pCharTmp, StrTmp.pChar);
	strcat(pCharTmp, S.pChar);
	delete [] StrTmp.pChar;
	StrTmp.pChar = new char [strlen(pCharTmp) + 1];
	strcpy(StrTmp.pChar,pCharTmp);
	return StrTmp;
	}


I now have:
1
2
3
4
5
String String::operator & (const String & S) const
	{
	String StrTmp (*this);
	return StrTmp.Concat(S);
	}


Thanks for your wonderful help...You guys are great! I spent an hour and a half with an online tutor at the school who couldn't figure it out...No offense to her,I am sure its not easy to glance at someones code and quickly figure it out, but you guys (& gals) can do it so my hat off to you.
Topic archived. No new replies allowed.