Re: Working with strings in c++
"delete[] str" is erroneous. It only makes sense in an assignment operator.
"John Carson" <jcarson_n_o_sp_am_@netspace.net.au> wrote in message
news:eaTpAl5SHHA.496@TK2MSFTNGP06.phx.gbl...
To illustrate Paul's point, try compiling this:
#include <cstring>
using namespace std;
class MyString
{
char *str;
public:
explicit MyString(const char* arg)
{
str = new char[strlen(arg)+1];
strcpy(str, arg);
}
MyString(const MyString& rhs)
{
delete[] str;
str = new char[strlen(rhs.str)+1];
strcpy(str, rhs.str);
}
};
int main()
{
MyString ms2 = "test";
return 0;
}
To illustrate my point, try compiling this using Comeau online
http://www.comeaucomputing.com/tryitout/
#include <cstring>
using namespace std;
class MyString
{
char *str;
public:
MyString(const char* arg)
{
str = new char[strlen(arg)+1];
strcpy(str, arg);
}
private:
MyString(const MyString& rhs)
{
delete[] str;
str = new char[strlen(rhs.str)+1];
strcpy(str, rhs.str);
}
};
int main()
{
MyString ms2 = "test";
return 0;
}
--
John Carson