Can I specify default value?

Posted by atch on Stack Overflow See other posts from Stack Overflow or by atch
Published on 2010-04-07T14:16:31Z Indexed on 2010/04/07 14:33 UTC
Read the original article Hit count: 258

Filed under:
|

Why is it that for user defined types when creating an array of objects every element of this array is initialized with default ctor but when I create built-in type this isn't the case? And second question: is it possible to specify default value to be used while initialize? Something like this (not valid):

char* p = new char[size]('\0');

And another question in this topic while I'm with arrays. I suppose that when creating an array of user defined type and knowing the fact that every elem. of this array will be initialized with default value firstly why?
If arrays for built in types do not initialize their elems. with their dflts why do they do it for UDT, and secondly: is there a way to switch it off/avoid/circumvent somehow? It seems like bit of a waste if I for example have created an array with size 10000 and then 10000 times dflt ctor will be invoked and I will (later on) overwrite this values anyway.
I think that behaviour should be consistent, so either every type of array should be initialized or none. And I think that the behaviour for built-in arrays is more appropriate.

© Stack Overflow or respective owner

Related posts about c++

Related posts about arrays