new 实例化对象的问题

cang_er03 2009-07-20 05:16:22
new 实例化对象,见过两种写法:
CDog *d=new CDog;
CDog *d=new CDog();
编译都能通过,有什么区别吗?
...全文
379 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
ziyouren4154 2009-07-20
  • 打赏
  • 举报
回复
申请整型空间是不能那样定义的,只有数组才能那样写。
amossavez 2009-07-20
  • 打赏
  • 举报
回复
有时间的话,你也可以看看这个帖子
http://topic.csdn.net/u/20090714/00/e7dad941-daf0-4905-a679-7a56ff412f77.html
amossavez 2009-07-20
  • 打赏
  • 举报
回复
不过要初始化的话,这种事最好不要让编译器给你做,自己最好显示的初始化
amossavez 2009-07-20
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 cang_er03 的回复:]
引用 7 楼 god_sun 的回复:
int *pi=new int(0)和int *pi=new int()是一样的

是吗?C/C++ codeint*pi=newint();
cout<<*pi<<endl;
那为什么我调试以上程序的结果为-842150451?
[/Quote]
没问题,codeint*pi=newint();会初始化为0;,我的编译器vs2008;输出为0;这个跟编译器有关
Aeris 2009-07-20
  • 打赏
  • 举报
回复
根据标准:

int *pi = new int; -> *pi的值不确定(5.3.4 - New 条款15)
int *pi = new int(); -> *pi执行默认初始化(5.3.4 - New 条款15),再根据默认初始化的条款(8.5 - Initializers 条款5),即为执行0初始化
kondykuang 2009-07-20
  • 打赏
  • 举报
回复
你既然 申请到了对象内存,
就应该自己去初始化它,不管编译器是否真的初始化为0
你在使用之前都应该手动初始化,
不要去纠结这些细节。new过来的对象 你把他看成是一块内存区域,而不是一个对象
ALLAN_ONLY 2009-07-20
  • 打赏
  • 举报
回复
我的倒是没问题
Aeris 2009-07-20
  • 打赏
  • 举报
回复
除此之外,还有5.3.4 new
5.3.4 - New

...省略若干

-15- A new-expression that creates an object of type T initializes that object as follows:

* If the new-initializer is omitted:

* If T is a (possibly cv-qualified) non-POD class type (or array thereof), the object is default-initialized If T is a const-qualified type, the underlying class type shall have a user-declared default constructor.

* Otherwise, the object created has indeterminate value. If T is a const-qualified type, or a (possibly cv-qualified) POD class type (or array thereof) containing (directly or indirectly) a member of const-qualified type, the program is ill-formed;

* If the new-initializer is of the form (), default-initialization shall be performed;
Aeris 2009-07-20
  • 打赏
  • 举报
回复
8.5 - Initializers

...省略若干

-5- To zero-initialize storage for an object of type T means:

* if T is a scalar type, the storage is set to the value of 0 (zero) converted to T;

* if T is a non-union class type, the storage for each nonstatic data member and each base-class subobject is zero-initialized;

* if T is a union type, the storage for its first data member is zero-initialized;

This member must not be static, by virtue of the requirements in class.union.

* if T is an array type, the storage for each element is zero-initialized;

* if T is a reference type, no initialization is performed.

To default-initialize an object of type T means:

* if T is a non-POD class type (clause class), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

* if T is an array type, each element is default-initialized;

* otherwise, the storage for the object is zero-initialized.
cang_er03 2009-07-20
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 aeris 的回复:]
查看C++标准的相关条款,记得这个问题之前已经有帖子讨论过了
[/Quote]
你能给我找出来吗?我找不到。
yusi10qi 2009-07-20
  • 打赏
  • 举报
回复
不是内置类型的关系吧,所以你不加数值就显示-842150451
Aeris 2009-07-20
  • 打赏
  • 举报
回复
查看C++标准的相关条款,记得这个问题之前已经有帖子讨论过了
cang_er03 2009-07-20
  • 打赏
  • 举报
回复
在vs2005中int *pi=new int(0)和int *pi=new int()是一样的,而在vc6.0中不一样。
以哪个为准,有依据吗?
cang_er03 2009-07-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 abcdef0966 的回复:]
引用 9 楼 cang_er03 的回复:
引用 7 楼 god_sun 的回复:
int *pi=new int(0)和int *pi=new int()是一样的

是吗?
int*pi=newint();
cout < <*pi < <endl;
那为什么我调试以上程序的结果为-842150451?


我这里也是,编译器问题?
[/Quote]
有人知道是什么原因吗?
ljt3969636 2009-07-20
  • 打赏
  • 举报
回复
类类型* p=new 类类型(); 与 类类型* p=new 类类型; 二者一样都会调用构造函数

int*p1=new int(); int*p2=new int;前者*p1会被初始化为0,后者*p2不作初始化是个随机数
abcdef0966 2009-07-20
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 cang_er03 的回复:]
引用 7 楼 god_sun 的回复:
int *pi=new int(0)和int *pi=new int()是一样的

是吗?C/C++ codeint*pi=newint();
cout<<*pi<<endl;
那为什么我调试以上程序的结果为-842150451?
[/Quote]

我这里也是,编译器问题?
cang_er03 2009-07-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 god_sun 的回复:]
int *pi=new int(0)和int *pi=new int()是一样的
[/Quote]
是吗?

int *pi=new int();
cout<<*pi<<endl;

那为什么我调试以上程序的结果为-842150451?
Yhzhtk 2009-07-20
  • 打赏
  • 举报
回复
CDog *d=new CDog();是系统默认的0,
int *pi=new int(0):这样是自己构造为0了
虽然数值一样,但含义有区别
god_sun 2009-07-20
  • 打赏
  • 举报
回复
int *pi=new int(0)和int *pi=new int()是一样的
cang_er03 2009-07-20
  • 打赏
  • 举报
回复
貌似不是这样的啊,int *pi=new int(0): //这样写,才是分配了一个int型对象,并用0将其初始化了
加载更多回复(5)

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧