c ++对象初始化和构造函数语义 [英] c++ object initialization and constructor semantics

查看:156
本文介绍了c ++对象初始化和构造函数语义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对象的2个初始化之间有区别。

Is there a difference between the 2 initailizations of an object.

Object obj(constructor_arguments);

Object obj = Object(constructor_arguments);

请注意,第二个初始化并不是指向 new 运算符。它是一个非堆变量。

Note that the second initialization is not intended to be a pointer with the new operator. It is intended to be a non heap variable.

在GCC中,编译和工作都很好,我想知道是否有任何差异,或者两个语句在语义上是否相同。

In GCC both compile and work fine and I'm wondering if there is actually any difference or if both statements are semantically the same.

推荐答案

是的。第一种是直接初始化的语法,第二种是复制初始化。

Yes there is. The first is the syntax for direct initialization, the second is copy initialization.

理论上,第二个函数调用复制构造函数,但这是可以优化的。

Theoretically, the second one calls the copy constructor, but this is subject to optimizations.

这篇关于c ++对象初始化和构造函数语义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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