triple fault
triple fault

Reputation: 14138

Array of objects - initialisation

Suppose I have a C++ class for whom I didn't write any constructor. What will be the difference between these 2 lines:

1. Complex* parray = new Complex[10]; 
2. Complex* parray2 = new Complex[10]();

Will behaviour will change if constructors will be provided.

Upvotes: 1

Views: 59

Answers (1)

juanchopanza
juanchopanza

Reputation: 227548

It depends on the type of Complex. If it is a POD, for example,

struct Complex
{
  double re, im;
};

then 1. will result in no initialization of the data members, and 2. will result in these being value-initialized, which means zero-initialized. If the data members are user-defined types, then their default constructor will be called in both cases:

struct Complex
{
  std::string re, im;
};

Upvotes: 6

Related Questions