C ++变量声明和初始化规则 [英] C++ variable declaration and initialization rules

查看:281
本文介绍了C ++变量声明和初始化规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下方式声明和初始化 C 类型的变量:

Consider the following ways of declaring and initializing a variable of type C:

C c1;

C c2;
c2 = C();

C c3(C());

C c4 = C();

这些都是完全相同的,或者其中一些根据确切的定义 C ? (假设它有公共默认和复制构造函数)。

Are all of these completely equivalent to each other, or can some of these differ depending on the exact definition of C? (assuming it has public default and copy constructors).

推荐答案

这些意思是:

C c1;   // default constructor

C c2;   // default constructor
c2 = C(); // default constructor followed by assignment

C c3(C());   // default constructor possibly followed by copy constructor

C c4 = C();  // default constructor possibly followed by copy constructor

注意编译器可以复制构造函数调用。它们是等效的吗? - 好,它取决于复制构造函数和赋值运算符。

Note the compiler can elide copy constructor calls. Are they equivalent? - well, it depends on what the copy constructor and assignment operator do.

这篇关于C ++变量声明和初始化规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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