01.04 C++核心准则C.50:如果在构造过程中需要“虚行为”,使用工厂函数

C.50: Use a factory function if you need "virtual behavior" during initialization
C.50:如果在构造过程中需要“虚行为”,使用工厂函数

Reason(原因)

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.

如果基类对象的状态必须依赖对象的派生部分,我们需要使用虚函数(或等价物)以便尽量压缩没有完美构造完成的对象被错误使用的时间窗口。

Note(注意)

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_prt;如果有些用法需要共享,调用者可以将unique_ptr移动到shared_ptr。然而,如果工厂的作者知道返回对象的所有的用法都是共享方式,可以返回shared_ptr并在函数体内部使用make_shared节约一次内存分配。

Example, bad(反面示例)

<code>class B {
public:
B() {
/* ... */
f(); // BAD: C.82: Don't call virtual functions in constructors and destructors
/* ... */
}

virtual void f() = 0;
};/<code>

Example(示例)

<code>class B {
protected:
class Token {};

public:
explicit B(Token) { /* ... */ } // create an imperfectly initialized object
virtual void f() = 0;

template<class>
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<class>
friend shared_ptr B::create();
};

shared_ptr p = D::create(); // creating a D object/<class>/<class>/<code>

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(),我们使(自由存储上的)构造过程可以便利地实施。

Note(注意)

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

觉得本文有帮助?请分享给更多人。

更多精彩文章请关注微信公众号【面向对象思考】!

面向对象开发,面向对象思考!