Difference between new test and new test ()

Source: Internet
Author: User

Http://stackoverflow.com/questions/620137/do-the-parentheses-after-the-type-name-make-a-difference-with-new

 

If 'test' is an ordinary class, is there any difference:

 
Test * test = new test;
// And
Test * test = new test ();

 

 

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, 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 shoshould 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 shoshould 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 callthe default ctor.

    • New C ()-value-initializes C, which callthe default ctor.

So in all versions of C ++ there's a difference"New"And"New ()"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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.