博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++核心准则C.48:如果构造函数需要用常数初始化成员,使用类内初始化器更合适
阅读量:2016 次
发布时间:2019-04-28

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

C.48: Prefer in-class initializers to member initializers in constructors for constant initializers

C.48:如果构造函数需要用常数初始化成员,使用类内初始化器更合适

 

Reason(原因)

Makes it explicit that the same value is expected to be used in all constructors. Avoids repetition. Avoids maintenance problems. It leads to the shortest and most efficient code.

明确表示希望所有的构造函数都使用相同的值。避免维护问题。可以生成最短,最高效的代码。

 

Example, bad(反面示例)

class X {   // BAD    int i;    string s;    int j;public:    X() :i{666}, s{"qqq"} { }   // j is uninitialized    X(int ii) :i{ii} {}         // s is "" and j is uninitialized    // ...};

 

How would a maintainer know whether j was deliberately uninitialized (probably a poor idea anyway) and whether it was intentional to give s the default value ""  in one case and qqq in another (almost certainly a bug)? The problem with j (forgetting to initialize a member) often happens when a new member is added to an existing class.

维护人员怎么才能知道 j 是否是故意没有初始化(尽管这可能是坏主意)呢?怎么知道一种情况将s初始化为“”,而另一种情况初始化为"qqq"是有意而为之呢(这几乎就是一个错误)?关于 j 的问题(忘了初始化某个成员)经常发生在向类添加新成员的时候。

 

Example(示例)

class X2 {    int i {666};    string s {"qqq"};    int j {0};public:    X2() = default;        // all members are initialized to their defaults    X2(int ii) :i{ii} {}   // s and j initialized to their defaults    // ...};

 

Alternative(可选方案)

We can get part of the benefits from default arguments to constructors, and that is not uncommon in older code. However, that is less explicit, causes more arguments to be passed, and is repetitive when there is more than one constructor:

通过使用构造函数的默认参数,我们可以得到某些好处。这种情况在老代码中可以说很常见。然而,这种做法缺乏明确性,会导致更多的参数被传递,并且在多于一个构造函数存在时导致代码重复,很麻烦。

class X3 {   // BAD: inexplicit, argument passing overhead    int i;    string s;    int j;public:    X3(int ii = 666, const string& ss = "qqq", int jj = 0)        :i{ii}, s{ss}, j{jj} { }   // all members are initialized to their defaults    // ...};

 

Enforcement(实施建议)

  • (Simple) Every constructor should initialize every member variable (either explicitly, via a delegating ctor call or via default construction).

    (简单)所有的构造函数都应该初始化每个成员(可以明确地通过委托构造函数或默认构造函数)

  • (Simple) Default arguments to constructors suggest an in-class initializer may be more appropriate.

  • (简单)针对构造函数的默认参数使用类内初始化器可能是更加恰当的选择。


 

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

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

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

你可能感兴趣的文章
【动态规划】每对顶点之间的最短路径之Floyd-Warshall算法
查看>>
2020BATJ面试系列:145个技术题高端技术题,助你拿offer!
查看>>
从简历被拒,到斩获字节跳动offer,这份学习集合功不可没!
查看>>
Android自定义view系列:手撸一个带点儿科技感的仪表盘!
查看>>
Flutter 学习路线图!跨平台开发必备,不可错过的Flutter进阶历程!
查看>>
使用libnet, libpcap模拟一个交换机
查看>>
AVR-GCC里定义的API
查看>>
关注地震,关爱灾民......
查看>>
Compiling FlightGear 1.0.0 with Visual C++.net 2005
查看>>
书法之美--篆书在线查询
查看>>
Win XP中通过安全策略关闭端口
查看>>
现在的linux内核是谁写的
查看>>
10年编程无师自通[转]
查看>>
10年编程无师自通[转]
查看>>
中国各省的简称及简称的由来
查看>>
i386和x86的区别
查看>>
[C++]构造函数与析构函数讲解
查看>>
怎样在拼打日语汉字时,在字上同时显示假名
查看>>
网上超强摘录
查看>>
C语言陷阱和缺陷
查看>>