C++核心准则C.50:如果在构造过程中需要“虚行为”,使用工厂函数
C.50:如果在构造过程中需要“虚行为”,使用工厂函数
If the state of a base class object must depend on the state of a derived part of the object, we need to use a virtual function (or equivalent) while minimizing the window of opportunity to misuse an imperfectly constructed object.
如果基类对象的状态必须依赖对象的派生部分,我们需要使用虚函数(或等价物)以便尽量压缩没有完美构造完成的对象被错误使用的时间窗口。
The return type of the factory should normally be unique_ptr by default; if some uses are shared, the caller can move the unique_ptr into a shared_ptr. However, if the factory author knows that all uses of the returned object will be shared uses, return shared_ptr and use make_shared in the body to save an allocation.
工厂的返回类型通常应该默认返回unique_ptr;如果有些用法需要共享,调用者可以将unique_ptr移动到shared_ptr。然而,如果工厂的作者知道返回对象的所有的用法都是共享方式,也可以返回shared_ptr,这样在函数体内部可以使用make_shared节约一次内存分配。
class B {
public:
B() {
/* ... */
f(); // BAD: C.82: Don't call virtual functions in constructors and destructors
/* ... */
}
virtual void f() = 0;
};
Example(示例)class B {
protected:
class Token {};
public:
explicit B(Token) { /* ... */ } // create an imperfectly initialized object
virtual void f() = 0;
template
static shared_ptr create() // interface for creating shared objects
{
auto p = make_shared(typename T::Token{});
p->post_initialize();
return p;
}
protected:
virtual void post_initialize() // called right after construction
{ /* ... */ f(); /* ... */ } // GOOD: virtual dispatch is safe
};
class D : public B { // some derived class
protected:
class Token {};
public:
explicit D(Token) : B{ B::Token{} } {}
void f() override { /* ... */ };
protected:
template
friend shared_ptr B::create();
};
shared_ptr p = D::create(); // creating a D object
make_shared requires that the constructor is public. By requiring a protected Token the constructor cannot be publicly called anymore, so we avoid an incompletely constructed object escaping into the wild. By providing the factory function create(), we make construction (on the free store) convenient.
make_shared调用的构造函数必须是公开的。通过要求一个保护的令牌保证构造函数不能被公开调用,这样我们就避免了不完全构造的对象流出。由于提供了一个工厂方法create(),(自由存储上的)构造过程可以便利地实施。
Conventional factory functions allocate on the free store, rather than on the stack or in an enclosing object.
常规的工厂方法在自由存储上分配对象内存,而不是在堆栈或者封闭的对象内。
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c50-use-a-factory-function-if-you-need-virtual-behavior-during-initialization
觉得本文有帮助?请分享给更多人。
关注【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!