每个类只有一个析构函数和一个赋值函数,但可以有多个构造函数(包含一个拷贝
构造函数,其它的称为普通构造函数)。对于任意一个类 A ,如果不想编写上述函数,
C++ 编译器将自动为A 产生四个缺省的函数,如
A(void); // 缺省的无参数构造函数
A(const A &a); // 缺省的拷贝构造函数
~A(void); // 缺省的析构函数
A & operate =(const A &a); // 缺省的赋值函数
这不禁让人疑惑,既然能自动生成函数,为什么还要程序员编写?
原因如下:
(1 )如果使用“缺省的无参数构造函数”和“缺省的析构函数”,等于放弃了自主“初
始化”和“清除”的机会,C++发明人 Stroustrup 的好心好意白费了。
(2 )“缺省的拷贝构造函数”和“缺省的赋值函数”均采用“位拷贝”而非“值拷贝”
的方式来实现,倘若类中含有指针变量,这两个函数注定将出错。
对于那些没有吃够苦头的 C++ 程序员,如果他说编写构造函数、析构函数与赋值函
数很容易,可以不用动脑筋,表明他的认识还比较肤浅,水平有待于提高。
本章以类String 的设计与实现为例,深入阐述被很多教科书忽视了的道理。String
的结构如下:
class String
{
public:
String(const char *str = NULL); // 普通构 造函数
String(const String &other); // 拷贝构 造函数
~ String(void); // 析构函数
String & operate =(const String &other); // 赋值函数
private:
char *m_data; // 用于保存字符串
};
class String
{
public:
String(void)
{
}
String(const char *str = NULL)
{
if (str == NULL)
{
m_data = new char[1];
*m_data = '\0';
}
else
{
int length = strlen(str);
m_data = new char[length + 1];
memset(m_data, '\0', sizeof(m_data));
strcpy(m_data, str);
}
}
String(const String &other)
{
int nLen = strlen(other.m_data);
m_data = new char[nLen + 1];
strcpy(m_data, other.m_data);
}
virtual ~String(void)
{
if (m_data != NULL)
{
delete [] m_data;
}
}
String & operator = (const String &other)
{
if (this == &other)
{
return *this;
}
delete []m_data;
int nLen = strlen(other.m_data);
m_data = new char[nLen + 1];
strcpy(m_data, other.m_data);
return *this;
}
private:
char *m_data;
};