node* n = new node; // 1
node* n = new node(); // 2
Can there be an implementation where the two would call different constructor?
1
is a default initialization, and 2
is a value initialization. What happens depends on the type of node
.
If it is a built-in type, the first version leaves the value of *n
un-initialized. The second results in zero-initialization.
If it is an aggregate, 1
would leave data members of built-in type uninitialized, 2
would zero-initialize them. Members of user defined types would get default constructed.
If it is a (non-aggregate) user defined type with a default constructor, the default constructor will be called in both cases.
Also, if I wanted to make multiple instances, i.e. new node [5];
Yes, unless you are stuck with a pre-C++11 compiler. For example,
struct Foo
{
int i;
Foo(int i) : i(i) {}
};
Foo* f = new Foo[3] {1, 2, 3};