1

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.

1 Answer 1

6

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;
};
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.