1. 要求对象产生于堆中
由于non-heap 对象会在定义时自动构造,并在寿命结束时自动析构,因此要阻止客户产生non-heap对象,只需要将构造或析构函数声明为private.又由于构造函数可能有多个,儿媳够函数只有一个,因此更好的选择是将析构函数声明为private,然后开放一接口调用它,像这样:
class UPNumber { public: UPNumber(); UPNumber(int initValue); UPNumber(double initValue); UPNumber(const UPNumber& rhs); // pseudo destructor,它是const menber function,因为const对象也需要被销毁 void destroy() const { delete this; } ... private: ~UPNumber(); };