I was bored today and I want to create my own little string class. I really like the 'System.String' class in .Net for it's Split/Replace/Remove etc. functions and I want to attempt to implement them.
My problem however is the destructor. If I have multiple instances of my string class that hold the same string, will all of them try and delete[] the same memory when the destructor is called??
For example:
void DoSomething() {
MyStringClass text = "Hello!";
{
MyStringClass text2 = text;
// Do something with text2
} // text2 is destroyed, but so is the char* string in memory
// text now points to useless memory??
}
Am I understanding this right? Lol.
Thanks,
Alex
EDIT:
Oops, I forgot to include the code:
class string {
unsigned int length;
char* text;
public:
string() : length(0), text(NULL) { }
string(const char* str) {
if (!str) throw;
length = strlen(str);
text = new char[length];
memcpy(text, str, length);
}
~string() {
delete[] text;
}
};