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.

asked Jan 3, 2014 at 13:27

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;
};
answered Jan 3, 2014 at 13:30
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.