class with pointer member
1. The three special functions
Copy constructor, copy assignment, destructor
2.ctor and dtor
inline String::String(const char* cstr = 0)
{
if(cstr)
{
m_data = new char[strlen(cstr)+1];
strcpy(m_data, cstr);
}
else
{
m_data = new char[1];
*m_data = '\0';
}
}
inline String::~String()
{
delete[] m_data;
}
{
String s1();
String s2("hello");
String* p = new String("hello");
delete p;
}
3. Pointers class must have copy constructors and copy assignment (Custom)
Deep and shallow copy copy
inline String& String::operator=(const String& str)
{
if(this == &str)
return *this;
delete[] m_data;
m_data = new char[strlen(str.m_data) + 1];
strcpy(m_data, str.m_data);
return *this;
}