Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Default construction of arrays of PODs using new
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Bjarke Hammersholt Roune, post: 4058615"] 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 [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Default construction of arrays of PODs using new
Top