class String
{
public:
String(constchar right[]);
String& operator= (const String& right);
int length() const;
private:
char* buffer;
int len;
};
int String::lenght() const {return len;}
String::String(constchar right[])
{
len = 0;
while (right[len] != '\0')
len++;
buffer = newchar[len+1];
for (int i = 0; i < len; i++)
buffer[i] = right[i];
buffer[len] = '\0';
}
String& String::operator= (const String& right)
{
int n = right.length();
for (int i = 0; i <= n; i++)
buffer[i] = right.buffer[i];
return *this;
}
Answer.
I have no clue... Could you help me?
The array size seems okay... new operator...
is it because of the dangling pointer because there is no delete operator?
You're right about the delete issue; there ought to be a destructor that handles that. I also noticed that in the operator equals we don't re-allocate memory for buffer so it may not be long enough to hold the entire contents of the String right.