My Simple String Implementation Gone Wrong
I am creating my simple 开发者_高级运维implementation of the String class like in the .Net framework. The only difference, it is less sugary if you know what I am mean. Anyhow, I got it everything to work except for the overloaded operators for the + operator; which end up displaying a garbage buffer. I still have not implemented exception handling nor garbage collecting. Here is the code below and the output if you are all curious.
#include <iostream>
using namespace std;
using namespace CDataTypes;
int _tmain(int argc, _TCHAR* argv[])
{
String* str = new String();
*str = "1 ";
String* str2 = new String();
*str2 = "2 ";
String* str3 = new String();
*str3 = "3 ";
String* str4 = new String("\nEnd!\n");
cout << str->GetText() << str2->GetText() << str3->GetText() << str4->GetText() << endl;
const char* s = *str + "H"; // Here is where it goes wrong!
String* cat = new String();
cout << "\n" << s << endl;
system("pause");
return 0;
}
Code where I think the problem lies:
const char* String::operator+(char obj[])
{
std::string str = v->at(this->id);
str += obj;
ptr = str.c_str();
return ptr;
}
const char* String::operator+(String obj)
{
std::string str = v->at(this->id);
str += v->at(obj.id);
ptr = str.c_str();
return ptr;
}
Here is the output:
1 2 3
End!
╠╠╠╠╠╠╠╠∟ ╠╠╠╠╠╠╠╠
Press any key to continue . . .
P.S I did some debugging and the variable ptr is not displaying garbage. So my question is how does it return garbage? The variable ptr is global by the way, if that info helps.
Your str
is a local variable in your operators - as soon as you return from the operator, str
is destroyed, and the pointer you return points to deleted memory (the local str
variable owns the memory that c_str()
returns a pointer to). If you e.g. return a std::string
instead of a const char *
the value in str
will be copied, and you can then use it in the calling function.
That said, normally operator+
returns a new object containing the result of an addition - you should likely create a new String
object and return that.
Your code doesn't work due to the way you handle/save the string:
You actually never modify the string that's assigned to current instance. The way you do it would work in C# (to some extent) but not in C++.
Let's do it step-by-step:
Assume this
's value (i'll just refer to it as left
) is "Hello", the other string's value (obj
- I'll call it right
) is "World".
The expected behaviour of operator +
would be to append right
to left
and return the whole string.
std::string str = v->at(this->id);
left
and right
are unchanged, str
now has the same value as left
.
str += v->at(obj.id);
left
and right
are unchanged,however
str` now contains "HelloWorld".
Now returning the pointer to str
is in theory the right thing, but as Erik mentioned already, this pointer won't be valid (it could still be valid if you're lucky, but don't assume this!).
Here's how I'd implement it:
String& String::operator+(String& other) // pass the other string by value to avoid it being copied!
{
v->at(this->id) += v->at(other->id); // append the string using std::string's + operator
return *this; // return this object
}
精彩评论