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
triple fault
14.4k8 gold badges36 silver badges53 bronze badges
1 Answer 1
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
juanchopanza
228k35 gold badges421 silver badges493 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-cpp