String類的構造函數,析構函數、拷貝構造函數和賦值函數


(1)構造函數
String::String(const char *str)
{
if(str==NULL)
{
  m_data  = new char[1];
  *m_data = ‘\0’; 
}
else
{
   Int length = strlen(str);
   m_data = new char[length];
   Strcpy(m_data,str);
}
}
(2)析構函數
String::~String(void)
{
   delete []m_data;
}
(3)拷貝構造函數
  String::String(const String &other)
{
Int length = strlen(other);
 m_data = new char[length+1];
Strcpy(m_data,other.m_data);
}
(4)賦值函數
String & String::operate =  (const String &other) 
 {
     If(this == &other)
       Return *this;
     Delete []m_data;
    m_data = NULL;
    M_data = new char[strlen(other.m_data)];
    Strcpy(m_data,other.m_data);
     Return *this;
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM