B
Bjarke Hammersholt Roune
I have what I think is a very simple question that everyone using C++
should know, yet I cannot find anywhere on the internet where it is
answered, including the FAQ, and I have been using C++ for many years,
and I don't know the answer for sure myself. That feels a bit
embarrassing, but I guess it's just too long ago I used arrays instead
of vector<X>. So now I am asking the question here.
Consider this code:
A* a = new A[10];
What I believe is that the 10 A's will be default-constructed if A is
a non-POD type, while the default-constructor will not be called if A
is a POD type, leaving an array of PODs uninitialized. So the A's
would probably need to be initialized separately if they are PODs,
even if A has a suitable default constructor, e.g. if A=int. Is this
true?
Now consider this code, which has () at the end:
A* a = new A[10]();
I've never seen this in actual code, and I can't find an explanation
of it on the internet. What I suspect from the few discussions of it I
have seen is that this will guarantee that the default constructor
will be called, no matter if A is a POD type or not. So e.g. if A=int,
this will initialize the elements of the array to zero. Is this true?
If no default constructor has been explicitly defined for A, then a
compiler-generated default constructor will be used, which will not
initialize any POD members (which will be all the members if A is a
POD), so the array will still be in need of initialization if A is a
POD that is not a built in type and for which no default constructor
has been defined. Is that right?
Thank you so much for any help you can give me on this.
Cheers
Bjarke Hammersholt Roune
should know, yet I cannot find anywhere on the internet where it is
answered, including the FAQ, and I have been using C++ for many years,
and I don't know the answer for sure myself. That feels a bit
embarrassing, but I guess it's just too long ago I used arrays instead
of vector<X>. So now I am asking the question here.
Consider this code:
A* a = new A[10];
What I believe is that the 10 A's will be default-constructed if A is
a non-POD type, while the default-constructor will not be called if A
is a POD type, leaving an array of PODs uninitialized. So the A's
would probably need to be initialized separately if they are PODs,
even if A has a suitable default constructor, e.g. if A=int. Is this
true?
Now consider this code, which has () at the end:
A* a = new A[10]();
I've never seen this in actual code, and I can't find an explanation
of it on the internet. What I suspect from the few discussions of it I
have seen is that this will guarantee that the default constructor
will be called, no matter if A is a POD type or not. So e.g. if A=int,
this will initialize the elements of the array to zero. Is this true?
If no default constructor has been explicitly defined for A, then a
compiler-generated default constructor will be used, which will not
initialize any POD members (which will be all the members if A is a
POD), so the array will still be in need of initialization if A is a
POD that is not a built in type and for which no default constructor
has been defined. Is that right?
Thank you so much for any help you can give me on this.
Cheers
Bjarke Hammersholt Roune