C ++对象初始化(堆栈) [英] C++ Object Initialization (Stack)

查看:710
本文介绍了C ++对象初始化(堆栈)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天看到一个c ++初始化一个我不熟悉的类。

I saw today a c++ Initialization of a class which I am not familiar with.

CPrice price = CPrice();

初始化通常应如下所示:

The Initialization should normally look like this

CPrice price;

我猜想第一个应该抛出一个错误或什么。
这里会发生什么?我想这个变量是在栈上,因为它没有用 new 初始化。

I would have guessed that the first one should throw an error or something. What happens here ? I would guess that the variable is on the stack because it wasn't initialized with new .

studio express 2012与微软的c ++编译器。是这可能是微软编译器特定的,因此允许的东西?

I use Visual studio express 2012 with microsofts c++ compiler. Is it maybe something which is microsoft compiler specific and thus allowed ?

推荐答案

两行都是完美的,从客户端代码角度来看,可观察的行为: price 是类型 CPrice 的默认构造变量,

Both lines are perfectly fine, and end up with the same observable behaviour from the client code point of view: price is a default constructed variable of type CPrice, of course allocated on the stack.

如果你想了解技术细节,它们是不一样的:

If you want to go into the technicalities, they are not identical:

CPrice price; 是<$ c类型的价格的默认初始化$ c> CPrice 。这是一个用户类型(即类),所以它总是意味着调用默认构造函数。

CPrice price; is a default initialization of a variable price of type CPrice. This is a user type (i.e., a class), so it always mean a call to the default constructor.

CPrice price = CPrice ; 是一个复合表达式,它执行两项操作:

CPrice price = CPrice(); is a compound expression that does two things:


  • CPrice通过直接初始化(它使用()调用一个构造函数)调用code>:initializes和anonymous CPrice code>)。

  • 然后复制初始化(在C ++ 11之前)/移动初始化(可用于C ++ 11以上)一个变量<复制源/移动对象为匿名 CPrice / code>实例

  • CPrice(): initializes and anonymous CPrice object (on the stack), by direct initialization (it invokes a constructor with the ()). Since the parentheses are empty, this will call the default constructor.
  • It then copy initializes (before C++11) / move initializes (available to C++11 onward) a variable price of type CPrice, the copied-from/moved-from object being the anonymous CPrice instance.

最长的分配强制为 CPrice ,否则代码会报错。但是,通过发出与最短形式相同的代码,允许编译器跳过复制构造并将其优化

此外,在C ++ 11中,如果一个移动构造函数存在 CPrice ,它将被用来代替复制构造函数在这种情况下(也就是说,如果这个操作没有被完全删除)。

The longest allocation forces that a copy constructor exists for CPrice, or the code will be in error. But the compiler is allowed to skip the copy construction and optimise it away, by issuing the same code than with the shortest form.
Additionally, in C++11, if a move constructor exists for CPrice, it will be used in place of the copy constructor in this case (that is, if this operation is not entirely removed anyway).

所以唯一可感知的区别是,即使 CPrice 不是可复制构造的,最短的形式也会编译。两种形式都要求 CPrice 为默认构造。

So the only perceivable difference is that the shortest form will compile even if CPrice is not copy constructible. Both forms require CPrice to be default constructible.

或多或少相关的精度,来自另一个答案。您可以认为像这样的假设的中间声明是一样的:

One more more or less related precision, coming from the other answer. You could think that an hypothetical middle ground declaration like this would be the same:

CPrice price();

但实际上完全不同:这个声明 price 是一个不带参数的函数(空括号),并返回一个 CPrice 。它通俗地称为最烦琐的解析

Yet, it is actually completely different: this one declares price to be a function taking no argument (the empty parentheses), and returning a CPrice. It is colloquially known as the most vexing parse.

这篇关于C ++对象初始化(堆栈)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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