-
concat overload +,-
In main I want to concatenate two strings
s1=s2+s3; // overloading +,=
Code:
String & String::operator=(const String & st)
{
if (this==&st)
return *this;
delete [] str;
len=st.len;
str new char[len+1];
strcpy(str,st.str);
return *this;
}
// assign a C string to a String
String & String::operator=(const char * s)
{
delete [] str;
len = strlen(s);
str=new char[len+1];
strcpy(str,s);
return *this;
}
//concatinate two strings
String operator+(const String &str1,const String &str2)
{
return strcat(str1.str,str2.str);
}
my questin concerns the data object that the pointer to char points to using the strcat function. Will this be destroyed when the overload function ends?
-
strcat() won't destroy either str1.str or str2.str, but you can destroy it/them in your function body if you so desire. str1 will change as result of call to strcat(), but str2 will not.
based on code posted the str member of class String appears to be just large enough to hold str, without any extra space allocated for additional possible chars. Therefore, I think you need to create a new str1.str with enough space to hold both str1 and str2 when concatenated.
obtain len of str1 and str2
declare temp string of length equal to str1 and str2 combined + 1.
copy str1.str to temp using strcpy.
delete str1.str
redeclare str1.str to be of same size as temp
copy temp back to str1.str using strcpy()
concatenate str2.str onto str1.str using strcat().
-
new concat
Here is the new code based on your alg. Elad
Thanks. Will you glance over it to see if I did it correctly
I also removed the const in the definiton in order to chage str1
and deleted the temp string.
Code:
String operator+(String &str1,const String &str2)
{
temp=new char[str1.len+str2.len+1]
strcpy(temp,str1.str);
//resize str1.str to hold concatenate string
delete [] str1.str;
str1.str=new char[strlen(temp)];
strcpy(str1.str,temp);
delete [] temp;
return strcat(str1.str,str2.str);
}
-
I think you need to declare a type for temp, probably char * like this:
char * temp=new char[str1.len+str2.len+1];
don't forget the semicolon at the end of the line. Reviewing it I guess temp only needs to be size str1.len + 1, since we aren't actually concatenating str2.str onto temp be copying temp back to str1.str. Oh well, giving temp the extra memory is of little consequence.
Then you need to make room for the null char at the end of temp when declaring the new size for str1, as strlen() only indicates the length of non null char in the string passed to it.
Given that you are sending two String objects to +, you probably should make the + operator a friend function rather than a class method in your class declaration.
There's also a missing = sign in the = overloading function body, probably a typo.
-
Here is the alternative you suggested
Code:
String operator+(String &str1,const String &str2)
{
int size=str1.len+str2.len+1
char * temp=new char[str1.len+1];
strcpy(temp,str1.str);
//resize str1.str to hold concatenate string
delete [] str1.str;
// had to create size before deletion of str1.str
str1.str=new char[size];
strcpy(str1.str,temp);
delete [] temp;
return strcat(str1.str,str2.str);// im worried about the return type
}
Here is my proto type
friend String operator+(String & str1,const String &str2);
finally String is a user defined class so returning a char * may not work or will the compiler type cast this to type String. Am i mistaken? Would the return type be String (user defined);determined by the first operand.
-
strcat(str1.str,str2.str);
return str1;