2

Possible Duplicate:
Do the parentheses after the type name make a difference with new?

Which operations are performed by the following code:

Test *t1 = new Test; // there is no () after new Test;

if there is a user declared default constructor?

What about this:

Test *t2 = new Test(); // there is () after new Test;
Andreas Haferburg
  • 5,189
  • 3
  • 37
  • 63

3 Answers3

7

They're equivalent, in both cases a ctor with no arguments will be called.

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
3

For structs and classes that have a constructor it's the same, the only difference between using the empty parentheses is for primitive types, which are zero-initialized if the parentheses are there, while they are left uninitialized otherwise.

Actually, it's more complicated than that; if you omit the parentheses:

  • non-POD classes and structs are default-initialized, which actually means that their constructor is called;
  • PODs (and in particular primitive types) are left not initialized;

If, instead, you specify the parentheses, the default-initialization is always performed, which, for primitive types, means zero-initialization.

The full story is explained at §5.3.4 ¶15; the default initialization is covered at §8.5.


Relevant standard quotation:

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 (8.5) 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 (8.5);
  • If the new-initializer is of the form (expression-list) and T is a class type, the appropriate constructor is called, using expression-list as the arguments (8.5);
  • If the new-initializer is of the form (expression-list) and T is an arithmetic, enumeration, pointer, or pointer-to-member type and expression-list comprises exactly one expression, then the object is initialized to the (possibly converted) value of the expression (8.5); — Otherwise the new-expression is ill-formed.

(§5.3.4 ¶15)

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
0

It automatically calls the default constructor. You may encounter these as well:

Test t1;
Test t3 = Test();

They all have the same effect of calling default constructor.

atoMerz
  • 7,534
  • 16
  • 61
  • 101
  • 2
    The second case is completely different to the rest: it declares a function that receives no arguments and returns a `Test`. The other two cases are again different, with the first "default-initializing" `t1`, and the last one "value-initializing" a temporary and copy-constructing `t3`. The difference between default and value initialization can be seen with POD types (default = uninitialized, value = zero-initialized), and while the copy will be elided by most/all compilers, the compiler has to check that there is an available copy-constructor (will fail if its been declared private) – David Rodríguez - dribeas May 04 '11 at 17:40
  • @David, +1 Thank you for pointing out, you're right about the second case, I edited my post. But about first one, I don't think there is any thing as uninitialized object, because constructor is always called so it is initialized if default constructor has been overloaded. I didn't mean to provide these example as identical to the ones in question, just said he might encounter these, and that these are all methods of calling default constructor, correct me if I'm wrong. – atoMerz May 05 '11 at 15:28