C++核心准则C.50:如果在构造过程中需要“虚行为”,使用工厂函数
发布日期:2021-07-01 05:26:35 浏览次数:3 分类:技术文章

本文共 2749 字,大约阅读时间需要 9 分钟。

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

 

Example, bad(反面示例)

 

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

 

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

 


 

觉得本文有帮助?欢迎点赞并分享给更多的人。

阅读更多更新文章,请关注微信公众号【面向对象思考】

转载地址:https://oothinking.blog.csdn.net/article/details/104578358 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:C++核心准则C.51:使用委托构造函数实现所有构造函数的共通动作
下一篇:C++核心准则C.49:构造函数中应该做的是初始化而不是赋值

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月21日 19时42分00秒