博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++核心准则C.50:如果在构造过程中需要“虚行为”,使用工厂函数
阅读量:2016 次
发布时间:2019-04-28

本文共 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

 


 

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

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

转载地址:http://qyixf.baihongyu.com/

你可能感兴趣的文章
Android--Error:Library projects cannot enable Jack. Jack is enabled in default config
查看>>
SpringBoot集成WebSocket,实现后台向前端推送信息
查看>>
MySQL学习笔记——慢查询
查看>>
私人定制!13个在线纹理生成资源网站
查看>>
HTML 一行显示
查看>>
JS 获取图片、文件数据并封装在json中
查看>>
Win10 快捷键
查看>>
读『开发App常见的九大错误』
查看>>
Freeline - Android平台上的秒级编译方案
查看>>
美股课堂:美国银行开户亲历记
查看>>
CairoSVG - Convert SVG to PNG or PDF - Contents
查看>>
“秀才造反,十年不成”是什么意思? - 已解决 - 搜搜问问
查看>>
2017年计划
查看>>
Zookeeper安装使用及JavaAPI使用
查看>>
vxworks
查看>>
推荐系统 - 基于物品本身的特征来(分类)推荐- 步骤与进阶的knn
查看>>
vector clear 内部过程
查看>>
Linux/Ubuntu下的用户切换
查看>>
周海汉:标准模板库stl 容器的线程安全
查看>>
python写的本地搜索小工具
查看>>