Object obj = Object()和Object obj()有什么区别? [英] What's difference between Object obj = Object() and Object obj()?

查看:588
本文介绍了Object obj = Object()和Object obj()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为它有很多信息,但我不知道这是怎么叫的。我不能理解下面两个代码字符串之间的区别:

  Object obj 

  Object obj = Object(); 

你能解释一下吗?或者至少说如何调用它。

解决方案

  Object obj 

不是对象的实例化,而是函数的声明 obj 其不带参数并返回 Object 的实例。

  Object obj; 

是一个默认初始化隐式构造函数(因此,默认隐式构造函数或用户定义的非显式无参数的构造函数),并且此声明调用非POD的隐式构造函数对象成员,对于POD类型,它不会初始化它们(它们不会被归零)。这适用于 Object 的成员递归递归。

  Object obj {}; 

列表初始化 (如果 Object 是聚合)。那些它调用不同,对于空大括号,行为是相同的:POD类型的所有成员是零初始化的,并且非POD是默认初始化的。

  Object obj = Object(); 

理论上是一个两步语句:1)create temporary Object instance; 2)然后通过复制构造函数/ move constructor / copy operator / move运算符构造obj。但在实践中,它将默认构造与复制/移动精度(它在所有现代编译器启用,即使所有优化关闭,你必须显式禁用elision)。



选择

p>

  Object obj; 

  Object obj {}; 

如果您希望快速初始化而不对其POD成员进行归零,请选择第一个。
如果您想要确保在 Object 实例化之后其所有POD成员都为零,请选择第二个。



实际上,在第一次读取成员之前,这两个变体在所有现代操作系统上的运行时速度都相同。



...



结束



使用值初始化:

  Object obj {}; 

,除非您需要在异国系统上实时表演。


I think that it has a lot of information about it but I don't know how this is called. I cannot understand difference between next two strings of code:

Object obj();

And

Object obj = Object();

Can you please explain? Or at least say how to call it.

解决方案

Object obj();

is not an instantiation of object, it is a declaration of a function obj which takes no arguments and returns an instance of Object.

Object obj;

is a default initialization, i.e. instantiation with implicit constructor (thus, default implicit constructor or user-defined non-explicit constructor with no parameters), and this declaration calls implicit constructors of non-POD Object members, and for POD-types it does not initialize them (they will not be zeroed). This is right for members of members of Object and so on recursively.

Object obj{};

is a list initialization or aggregate initialization (if Object is an aggregate). Those it called differently, for empty braces, behavior is the same: all members of POD-types are zero-initialized, and non-POD are default-initialized.

Object obj = Object();

theoretically is a two-step statement: 1) create temporary Object instance; 2) then construct obj by copy constructor/move constructor/copy operator/move operator. But in practice it will be default-constructed with copy/move-elision in mind (it is enabled on all modern compilers by default even with all optimizations off, you must disable elision explicitly). Better do not use this variant.

Pre-Conclusion

Choose

Object obj;

or

Object obj{};

Choose first if you want fast initialization with no zeroifying its POD-members. Choose second if you want to be sure that all its POD-members will be zero after instantiation of Object.

Practically, before first reading from its members, the both variants have the same speed in runtime on all modern OSes.

So...

Conclusion

Use value-initialization:

Object obj{};

unless you need a realtime performance on exotic systems.

这篇关于Object obj = Object()和Object obj()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆