考點:構造函數、析構函數和賦值函數的編寫方法
出現頻率:☆☆☆☆☆
已知類String的原型為:
class String
{
public:
String(const char *str = NULL); //普通構造函數
String(const String &other); //拷貝構造函數
~ String(void); //析構函數
String & operator =(const String &other); //賦值函數
private:
char *m_String; //私有成員,保存字符串
};
解析:
程序代碼如下:
#include <iostream>
using namespace std;
class String
{
public:
String(const char *str = NULL); //普通構造函數
String(const String &other); //拷貝構造函數
~ String(void); //析構函數
String & operator =(const String &other); //賦值函數
private:
char *m_String; //私有成員,保存字符串
};
String::~String(void)
{
cout << "Destructing"<< endl;
if (m_String != NULL) //如果m_String不為NULL,釋放堆內存
{
delete [] m_String;
m_String = NULL; //釋放后置為NULL
}
}
String::String(const char *str)
{
cout << "Construcing" << endl;
if(str == NULL) //如果str為NULL,存空字符串""
{
m_String = new char[1]; //分配一個字節
*m_String = '\0'; //將之賦值為字符串結束符
}
else
{
m_String = new char[strlen(str) + 1]; //分配空間容納str內容
strcpy(m_String, str); //拷貝str到私有成員
}
}
String::String(const String &other)
{
cout << "Constructing Copy" << endl;
m_String = new char[strlen(other.m_String) + 1]; //分配空間容納str內容
strcpy(m_String, other.m_String); //拷貝str到私有成員
}
String & String:perator = (const String &other)
{
cout << "Operate = Function" << endl;
if(this == &other) //如果對象與other是同一個對象
{ //直接返回本身
return *this;
}
delete [] m_String; //釋放堆內存
m_String = new char[strlen(other.m_String)+1];
strcpy(m_String, other.m_String);
return *this;
}
int main()
{
String a("hello"); //調用普通構造函數
String b("world"); //調用普通構造函數
String c(a); //調用拷貝構造函數
c = b; //調用賦值函數
return 0;
}
1)普通構造函數:這里判斷了傳入的參數是否為NULL。如果是NULL,初始化一個字節的空字符串(包括結束符'\0');如果不是,分配足夠大小長度的堆內存保存字符串。
(2)拷貝構造函數:只是分配足夠小長度的堆內存保存字符串。
(3)析構函數:如果類私有成員m_String不為NULL,釋放m_String指向的堆內存,並且為了避免產生野指針,將m_String賦為NULL。
(4)賦值函數:首先判斷當前對象與引用傳遞對象是否是同一個對象,如果是,不做操作直接返回;否則先釋放當前對象的堆內存,然后分配足夠大小長度的堆內存拷貝字符串。
程序的執行結果如下:
Construcing
Construcing
Construcing Copy
Operate = Function
Destructing
Destructing
Destructing
這里代碼第63~66行會發生構造函數以及賦值函數的調用,而析構函數的調用發生在main()函數退出時
