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.