在类型名称后面的括号是否与新的有所不同? [英] Do the parentheses after the type name make a difference with new?

查看:338
本文介绍了在类型名称后面的括号是否与新的有所不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果'Test'是普通类,是否有任何差异:

  Test * test = new Test; 

  Test * test = new Test(); 


解决方案

影响你的代码的行为。以下大部分摘自对旧新Thing第条。



有时新操作符返回的内存将被初始化,有时它不会取决于你是新的类型up是 POD(普通旧数据),或者如果它是一个类




  • 在C ++ 1998中有两种类型的初始化:zero和default

  • 在C ++ 2003中添加了第三种初始化类型,值初始化。



假设:

  struct A {int m; }; // POD 
struct B {〜B(); int m; }; //非POD,编译器生成的默认ctor
struct C {C():m(){}; 〜C(); int m; }; // non-POD,default-initialising m

在C ++ 98编译器中,发生:




  • new A - 不确定值

  • new A()


  • new B - 默认构造(B :: m未初始化)


  • p>

  • new C() - 默认构造(C :: m为零初始化)



在C ++ 03一致的编译器中,事情应该像这样工作:




  • new A - 不确定值

  • new A() - value-initialize A,它是一个POD的零初始化。


  • 默认初始化(叶子B :: m未初始化)


  • new B() - 值初始化B,


  • new C - 默认初始化C,调用默认ctor。

    $ b
  • new C() - 初始化C,调用默认的ctor。



new A new A()之间有一个区别,因为A是POD。



对于 new B()的情况,C ++ 98和C ++ 03之间的行为存在差异。



这是C ++的一个灰尘的角落,可以驱使你疯狂。当构造一个对象时,有时你想要/需要的括号,有时你绝对不能有它们,有时它不重要。


If 'Test' is an ordinary class, is there any difference between:

Test* test = new Test;

and

Test* test = new Test();

解决方案

Let's get pedantic, because there are differences that can actually affect your code's behavior. Much of the following is taken from comments made to an "Old New Thing" article.

Sometimes the memory returned by the new operator will be initialized, and sometimes it won't depending on whether the type you're newing up is a POD (plain old data), or if it's a class that contains POD members and is using a compiler-generated default constructor.

  • In C++1998 there are 2 types of initialization: zero and default
  • In C++2003 a 3rd type of initialization, value initialization was added.

Assume:

struct A { int m; }; // POD
struct B { ~B(); int m; }; // non-POD, compiler generated default ctor
struct C { C() : m() {}; ~C(); int m; }; // non-POD, default-initialising m

In a C++98 compiler, the following should occur:

  • new A - indeterminate value
  • new A() - zero-initialize

  • new B - default construct (B::m is uninitialized)

  • new B() - default construct (B::m is uninitialized)

  • new C - default construct (C::m is zero-initialized)

  • new C() - default construct (C::m is zero-initialized)

In a C++03 conformant compiler, things should work like so:

  • new A - indeterminate value
  • new A() - value-initialize A, which is zero-initialization since it's a POD.

  • new B - default-initializes (leaves B::m uninitialized)

  • new B() - value-initializes B which zero-initializes all fields since its default ctor is compiler generated as opposed to user-defined.

  • new C - default-initializes C, which calls the default ctor.

  • new C() - value-initializes C, which calls the default ctor.

So in all versions of C++ there's a difference between "new A" and "new A()" because A is a POD.

And there's a difference in behavior between C++98 and C++03 for the case "new B()".

This is one of the dusty corners of C++ that can drive you crazy. When constructing an object, sometimes you want/need the parens, sometimes you absolutely cannot have them, and sometimes it doesn't matter.

这篇关于在类型名称后面的括号是否与新的有所不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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