1、当创建一个类时,C++编译器将默认产生下列函数:
A(void); // 缺省的无参数构造函数
A(const A &a); // 缺省的拷贝构造函数
~A(void); // 缺省的析构函数
A & operate =(const A &a); // 缺省的赋值函数
“缺省的拷贝构造函数”和“缺省的赋值函数”均采用“位拷贝”而非“值拷贝”的方式来实现,倘若类中含有指针变量,这两个函数注定将出错。
2、构造函数初始化表的使用规则
如果类存在继承关系,派生类必须在其初始化表里调用基类的构造函数。如:
class A
{…
A(int x); // A 的构造函数
};
class B : public A
{…
B(int x, int y);// B 的构造函数
};
B::B(int x, int y)
: A(x) // 在初始化表里调用 A 的构造函数
{
…
}
类的 const 常量只能在初始化表里被初始化,因为它不能在函数体内用赋值的方式来初始化。
类的数据成员的初始化可以采用初始化表或函数体内赋值两种方式,如:
class A
{…
A(void); // 无参数构造函数
A(const A &other); // 拷贝构造函数
A & operate =( const A &other); // 赋值函数
};
class B
{
public:
B(const A &a); // B 的构造函数
private:
A m_a; // 成员对象
};
//成员对象在初始化表中被初始化
B::B(const A &a)
: m_a(a)
{
…
}
//成员对象在函数体内被初始化
B::B(const A &a)
{
m_a = a;
…
}
对于内部数据类型的数据成员而言,两种初始化方式的效率几乎没有区别
而非内部数据类型的成员对象应当采用第一种方式初始化,以获取更高的效率。
3、 拷贝构造函数和赋值函数非常容易混淆,如:
String a(“hello”);
String b(“world”);
String c = a; // 调用了拷贝构造函数,最好写成 c(a);
c = b; // 调用了赋值函数
本例中第三个语句的风格较差,宜改写成 String c(a) 以区别于第四个语句。
4、 派生类的构造函数应在其初始化表里调用基类的构造函数。
基类与派生类的析构函数应该为虚(即加 virtual 关键字) 。如
#include <iostream.h>
class Base
{
public:
virtual ~Base() { cout<< "~Base" << endl ; }
};
class Derived : public Base
{
public:
virtual ~Derived() { cout<< "~Derived" << endl ; }
};
void main(void)
{
Base * pB = new Derived; // upcast
delete pB;
}
输出结果为:
~Derived
~Base
如果析构函数不为虚,那么输出结果为
~Base ,派生类的构造函数被隐藏了。
5、 在编写派生类的赋值函数时,注意不要忘记对基类的数据成员重新赋值。
class Base
{
public:
…
Base & operate =(const Base &other); // 类 Base 的赋值函数
private:
int m_i, m_j, m_k;
};
class Derived : public Base
{
public:
…
Derived & operate =(const Derived &other); // 类 Derived 的赋值函数
private:
int m_x, m_y, m_z;
};
Derived & Derived::operate =(const Derived &other)
{
//(1)检查自赋值
if(this == &other)
return *this;
//(2)对基类的数据成员重新赋值
Base::operate =(other); // 因为不能直接操作私有数据成员
//(3)对派生类的数据成员赋值
m_x = other.m_x;
m_y = other.m_y;
m_z = other.m_z;
//(4)返回本对象的引用
return *this;
}