前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则C.50:如果在构造过程中需要“虚行为”,使用工厂函数

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

作者头像
面向对象思考
发布2020-03-25 16:13:36
6040
发布2020-03-25 16:13:36
举报
文章被收录于专栏:C++核心准则原文翻译

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(反面示例)

代码语言:javascript
复制
class B {
public:
    B() {
        /* ... */
        f(); // BAD: C.82: Don't call virtual functions in constructors and destructors
        /* ... */
    }

    virtual void f() = 0;
};

Example(示例)

代码语言:javascript
复制
class B {
protected:
    class Token {};

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

    template<class T>
    static shared_ptr<T> create()    // interface for creating shared objects
    {
        auto p = make_shared<T>(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 T>
    friend shared_ptr<T> B::create();
};

shared_ptr<D> p = D::create<D>();  // 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


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

关注【面向对象思考】轻松学习每一天!

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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-01-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档