继续Effective C++之旅,这一节围绕构造/析构/赋值三个方面谈实践中需要遵守的规则。
【条款05】了解C++默默编写并调用哪些函数
编译器可以暗自为class创建default构造函数、copy构造函数、copy assignment操作符,以及析构函数
【条款06】若不想使用编译器自动生成的函数,就该明确拒绝
为驳回编译器自动(暗自)提供的机能,可将相应的成员函数声明为private并且不予实现。标准库实现代码中ios_base,base_ios和sentry,为了阻止copying行为,其构造函数和copy assignment操作符都被声明为private而且没有定义。
class HomeForSale {
public:
...
private:
...
HomeForSale(const HomeForSale&);
HomeForSale& operator=(const HomeForSale&);
};
使用像Uncoyable这样的base class也是一种做法。
class Uncopyable {
protected:
Uncopyable() {}
~Uncopyable() {}
private:
Uncopyable(const Uncopyable&);
Uncopyable& operator=(const Uncopyable&);
};
class HomeForSale: private Uncopyable {
...
};
【条款07】为多态基类声明virtual析构函数
任何class只要带有virtual函数都几乎确定应该也有一个virtual析构函数。如果class不含virtual函数,通常表示它并不意图被用作一个base class。当class不企图被当做base class,令其析构函数为virtual往往是个馊主意。
析构函数的运作方式是,最深层派生(most derived)的那个class其析构函数最先被调用,然后是其每一个base class的析构函数被调用。
polymorphic(带多态性质的)base class应该声明为一个virtual析构函数。如果class带有任何virtual函数,它就应该拥有一个virtual析构函数。
classes的设计目的如果不是作为base classes使用,或不是为了具备多态性(polymorphically),就不该声明virtual析构函数。
【条款08】别让异常逃离析构函数
析构函数绝对不要吐出异常。如果一个被析构函数调用的函数可能抛出异常,析构函数应该捕捉任何异常,然后吞下它们(不传播)或结束程序。
如果客户需要对某个操作函数运行期间抛出的异常做出反应,那么class应该提供一个普通函数(而非在析构函数中)执行该操作。
【条款09】绝不在构造和析构函数中调用virtual函数
确定你的构造函数和析构函数都没有(在对象被创建或被销毁期间)调用virtual函数,而它们调用的任何函数也都服从同一约束。
在构造和析构期间不要调用virtual函数,因为这类调用从不下降至derived class(比起当前执行构造函数和析构函数的那层)。
【条款10】令operator=返回一个reference to *this
令赋值(assignment)操作符返回一个reference to *this
class Widget {
public:
...
Widget& operator=(const Widget& rhs)
{
...
return *this;
}
...
};
【条款11】在operator=中处理“自我赋值”
确保当前对象自我赋值时operator=有良好行为。其中技术包括比较“来源对象”和“目标对象”的地址、
class Bitmap {…};
class Widget {
...
private:
Bitmap* pb;
};
// not safe, &rhs may point to this.
Widget& Widget::operator=(const Widget& rhs)
{
delete pb;
pb = new Bitmap(*rhs.pb);
return *this;
}
// using identity test
Widget& Widget::operator=(const Widget& rhs)
{
if (this == &rhs) return *this;
delete pb;
pb = new Bitmap(*rhs.pb);
return *this;
}
精心周到的语句顺序、
Widget& Widget::operator=(const Widget& rhs)
{
Bitmap* pOrig = pb;
pb = new Bitmap(*rhs.pb);
delete pOrig;
return *this;
}
以及copy-and-swap。
class Widget{
...
void swap(Widget& rhs); // swap data of *this and rhs
...
};
Widget& Widget::operator=(const Widget& rhs)
{
Widget temp(rhs);
swap(temp);
return *this;
}
Widget& Widget::operator=(Widget rhs)
{
swap(rhs);
return *this;
}
确定任何函数如果操作一个以上的对象,而其中多个对象是同一个对象时,其行为仍然正确。
class Base { … };
class Derived: public Base { … };
void doSomething(const Base& rb, // rb and *pd may be the same object
Derived* pb);
【条款12】复制对象时勿忘其每一个成分
Copying函数应该确保复制“对象内的所有成员变量”以及“所有base class成分”。
class Customer {
public:
...
Customer(const Customer& rhs)
: name(rhs.name)
{ … }
Customer& operator=(const Customer& rhs)
{
...
name = rhs.name;
...
return *this;
}
...
private:
std::string name;
};
class PriorityCustomer: public Customer {
public:
...
PriorityCustomer(const PriorityCustomer& rhs)
: Customer(rhs),
priority(rhs.priority)
{
...
}
PriorityCustomer& operator=(const PriorityCustomer& rhs)
{
...
Customer::operator=(rhs);
priority = rhs.priority;
return *this;
}
};
不要尝试以某个copying函数实现另一个copying函数。应该将共同机能放进第三个函数中,并由两个copying函数共同调用。
参考:Effective C++:改善程序与设计的55个具体做法(第三版) Scott Meyers著 侯捷译