新X与新X() [英] new X vs new X()

查看:98
本文介绍了新X与新X()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


新X和新X()之间有什么区别吗?!


问候,

Razvan

Hi !

Is there any difference between new X and new X() ?!

Regards,
Razvan

推荐答案

Razvan写道:
新X和新X()之间有什么区别吗?
Is there any difference between new X and new X() ?!




曾经有过,但前提是X是POD。第一种形式将

保持未初始化状态,第二种形式默认初始化它。


现在我不确定。


V



There used to be, but only if X is a POD. The first form would
leave it uninitialised, the second would default-initialise it.

Nowadays I am not sure.

V


" Razvan" < MI ***** @ mailcity.com>在消息中写道

news:15 ************************* @ posting.google.co m ...
"Razvan" <mi*****@mailcity.com> wrote in message
news:15*************************@posting.google.co m...
新X和新X()之间有什么区别吗?!
Is there any difference between new X and new X() ?!




是的,但你很少见应该关心。


如果你说new X,分配对象的初始值与

相同,是未初始化的局部变量类型会有。因此,对于

示例,new int的结果为是未初始化的int的地址,并且

是new std :: string的结果。是std :: string对象的地址,其中

没有字符(因为所有字符串都是初始化的,如果只是默认的
构造函数)。相反,new int()的结果是new int()的结果。是值为0的

int的地址,以及new std :: string()的结果。与new std :: string结果完全相同

(因为所有字符串都被初始化,如果只有默认构造函数的话,只需要
。)

这是一个相对较新的功能,所以如果你写一个依赖的程序

,你可能希望确保你的编译器正确实现它。



Yes, but it is very rare that you should care.

If you say "new X", the initial value of the object allocated is the same as
an uninitialized local variable of the same type would have. So, for
example, the result of "new int" is the address of an uninitialized int, and
the result of "new std::string" is the address of a std::string object with
no characters (because all strings are initialized, if only by the default
constructor). In contrast, the result of "new int()" is the address of an
int with value 0, and the result of "new std::string()" is exactly the same
as the result of "new std::string" (because all strings are initialized, if
only by the default constructor).

This is a relatively recent feature, so if you write a program that relies
on it, you may wish to make sure that your compiler implements it correctly.


Victor Bazarov在新闻中写道:jB **** **************@dfw-read.news.verio.net
comp.lang.c ++中的

Victor Bazarov wrote in news:jB******************@dfw-read.news.verio.net
in comp.lang.c++:
Razvan写道:
新X和新X()之间有什么区别吗?!
Is there any difference between new X and new X() ?!



曾经有过,但只有X是POD 。第一种形式将保持未初始化,第二种形式默认初始化它。

现在我不确定。



There used to be, but only if X is a POD. The first form would
leave it uninitialised, the second would default-initialise it.

Nowadays I am not sure.



现在它的价值 - 初始化,这是一种递归的

默认初始化。


8.5 / 5



?如果T是具有用户声明的

构造函数(12.1)的类类型(第9节),则调用T

的默认构造函数(并且初始化生病了 - 如果T

没有可访问的默认构造函数);


?如果T是没有用户声明的

构造函数的非联合类类型,那么T的每个非静态数据成员和

基类组件都是值初始化的;


8.5 / 7

一个对象,其初始化程序是一组空的括号,

即,( ),应进行价值初始化。


Rob。

-
http://www.victim-prime.dsl.pipex.com/

这篇关于新X与新X()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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