operator overloading

Nov 20, 2011 at 12:44pm
This is the program for performing concatenation using overloading + operator. The problem with the code is it is not printing concatenated string.

#include<cstring>
#include<iostream>
using namespace std;
class mystring
{
char *str;
int size;
public:
mystring()
{
str=NULL; size=0;
}
mystring(mystring &a)
{
size=a.size;
str=new char[size+1];
strcpy(str,a.str);
}

mystring(char *s)
{

size=strlen(s);
str=new char[size+1];
strcpy(str,s);
}
friend mystring operator+(mystring &,mystring &);

void display();
~mystring()

{ this->display();
delete []str;
}


}t;
mystring operator+(mystring &a,mystring &b)
{


t.size=a.size+b.size;
t.str=new char[t.size+1];
strcpy(t.str,a.str);
strcat(t.str,b.str);
return t;
}
void mystring::display()
{
cout<<str;
}
int main()
{
char s[100]={NULL};
cout<<"\n ENTER STRING 1: ";
cin>>s;
mystring s1(s);
cout<<"\n ENTER STRING 2: ";
cin>>s;
mystring s2(s);

s1=s1+s2;
cout<<"\n CONCATINATING STRING : ";
s1.display();
cout<<endl;

}
A sample run to this problem is as below:


SAMPLE OUTPUT
ENTER STRING 1: xyz

ENTER STRING 2: abc

CONCATINATING STRING :


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.
Nov 20, 2011 at 5:46pm
you need to overload assign operator as well.

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
#include<cstring>
#include<iostream>

using namespace std;

class mystring
{
char *str;
int size;
public:
mystring()
{
str=NULL; size=0;
}
mystring(const mystring &a)
{
size=a.size;
str=new char[size+1];
strcpy(str,a.str);
}

mystring& operator=( const mystring &rhs)
{
if (this==&rhs)
return *this;

size=rhs.size;
str=new char[size+1];
strcpy(str,rhs.str);
return *this;
}
mystring(char *s)
{

size=strlen(s);
str=new char[size+1];
strcpy(str,s);
}
friend mystring operator+(const mystring &,const mystring &);

void display();
~mystring()

{ this->display();
delete []str;
}


}t;

mystring operator+(const mystring &a,const mystring &b)
{

//mystring t1;
t.size=a.size+b.size;
t.str=new char[t.size+1];
strcpy(t.str,a.str);
strcat(t.str,b.str);
return t;
}
void mystring::display()
{
cout<<str;
}
int main()
{
char s[100];
memset(s, '\0', sizeof(s));
cout<<"\n ENTER STRING 1: ";
cin>>s;
mystring s1(s);
cout<<"\n ENTER STRING 2: ";
cin>>s;
mystring s2(s);
//mystring s3;
s1=s1+s2;
cout<<"\n CONCATINATING STRING : ";
s1.display();
cout<<endl;

}

Nov 21, 2011 at 4:38am
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...??
Nov 21, 2011 at 6:09am
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.
Nov 21, 2011 at 7:15am
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.
Nov 21, 2011 at 9:25am
thank you andywestken to elaborate on this.
Nov 21, 2011 at 3:06pm
thanks both of u for helping me out.... :)
Topic archived. No new replies allowed.