简体   繁体   English

0 C ++内置类型的初始化

[英]0 initialization of C++ built-in types

suppose I have this struct (or class, my question applies to both): 假设我有这个结构(或类,我的问题对两个都适用):

struct builtin 
{ 
    int a;
    int b; 
    builtin() : a(), b(0) { } 
};

I know that both a and b will be initialized to 0 by the constructor of builtin. 我知道a和b都会被内建的构造函数初始化为0。 My question is: Is one method faster than the other? 我的问题是:一种方法比另一种更快吗?

They do the same and take the same amount of time. 它们执行相同的操作并花费相同的时间。 Also, optimizations on this level are pointless until a profiler proves the opposite. 同样,在此层次上的优化是没有意义的,直到探查器证明相反为止。 Use what's more readable to you. 使用对您更具可读性的内容。

Answer: no. 答:不可以。 The compiled code is identical. 编译后的代码是相同的。

There is no difference in the resulting machine code, only in readability. 生成的机器代码没有区别,只是可读性不同。 Here, a is default initialized while b is set to a specific value. 在此,默认将a初始化,而b设置为特定值。

Is that important to show in the source code? 在源代码中显示那很重要吗? Chose the one that make most sense! 选择最有意义的一个!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM