|
ClassString
{
Private:
char* m_pData;
unsigned m_nlength;
static unsigned s_nCount;
Public:
String(char* 52); //default ctor
String(char* 52);
String (const String& s); //copy ctor
~String(); |
int main ( )
{
String s1("this is a test");
String s2(s1);
return 0;
}
|
|
String::String(const, String& s) //s is not modifyable
{
s_nCount++;
m_nlength = s.m_nLength;
m_pData = s. m_pData;
}
String:: ~String()
{
delete [ ] m_pData;
} |
String::String(char* szstr)
{
n_nLength = strlen(szStr);
if(n_nLength ==0)
{
n_pData = NULL;
}
else
{
n_pData = new char [n_nLength +1];
strcpy(m_pData, szStr);
}
} |
|
Shallow Copy:
//if this program were to run, the data pointed to by S1 would be delected by the constructor, but S2 would still be pointing to it, (but there’s no data)
Shallow copies require sophistocated internal data structors to ensure that memory is properly managed . The easiest solution is to make these methods deep.
Deep Copy:
When an object allocate heap memory, any copy constructor or assignment operator almost always should allocate and copy these data members. This is known as making a deep copy. |
Rule of the Big Three:
the "Big Three" are:
-copy ctor
-assignment operator
-dtor
If you override any of the "Big Three" because of memory allocation (ie. deep copies), almost always you must override the other two. |
|
ClassString
{
Private:
char* m_pData;
unsigned m_nlength;
static unsigned s_nCount;
Public:
String();
String(char* 52);
String (const String& s);
~String();
String& operator = (const String& s); |
int main ( )
{
String s1("this is a test");
String s2 = S1;
String s3(S2);
int x = 7;
int y = 5;
|
|
String::String(const, String& s) //s is not modifyable
{
s_nCount++;
m_nlength = s..m_nLength;
m_szData = new char [m_nLength +1];
strncpy(n_szData, s.m_szData);
}
String& String::operator(const String&s);
{
if (&s == this) //this: pointer to the inplied object
return *this;
}
delete [] m_pData;
m_pData = new char [s.m_nLength +1];
strcpy (n_pData, s.m_pData);
n_nLength = s. m_nLength;
return *this;
} |
String::String(char* szstr)
{
n_nLength = strlen(szStr);
if(n_nLength ==0)
{
n_pData = NULL;
}
else
{
n_pData = new char [n_nLength +1];
strcpy(m_pData, szStr);
}
} |
|
ClassString
{
Private:
char* m_pData;
unsigned m_nlength; //the length of the string
unsigned m_nSize; // the allocated length of the pointer
char * m_pData;
static unsigned s_nCount;
Public:
String& operator +=(const String& s);
String& operator +=(const char c);
bool operator == (const String& s) const;
bool operator != (const String$ s)const; |
int main ( )
{
String s1("this is a test");
String s2 = S1;
String s3(S2);
String S4;
S4 = S3;
S4 += S2;
S4 += ‘s’;
{ |
|
String& String::operator +=(const string&s)
{
//T H I S I S A T E S T \o
15 characters (incl. null), allocate the amount, then copy each time you want to add a new character. inefficients, so: increase the size of the array to accomodate additions
Sring& String::operator +=(const char ch)
{
if m_msize == m_mlength)
{
m_msize += 20;
char* ptemp = new char [m_nSize +1];
strcpy (ptemp, m_pData);
delete [ ] m_pData;
m_pData = ptemp
}
m_pData [m_nLength] =ch;
m_nLength ++;
m_pData [m_mLength] = ‘\0’;
return *this;
} |
String ::String() //default constructor, does nothing
except initalize the size of the string to 0.
{
m_nLength = 0;
m_nSize = 0;
m_pData = NULL;
s_nCount++;
}
String ::String (const char* szStr)
{
if (szStr == Null)
{
m_nLength = 0;
}
m_nLength = strnlen(szStr);
m-nSize = m_nLength + 20;
m_pData = new char [m_mSize +1];
strcpy(m_pData, szStr); |