Doubt in default constructor

Jun 17, 2014 at 8:59am
Hi everybody,

I have started studying thinking in c++ , in the chapter "Initialization and cleanup" I have doubt .The author says "you might be thinking the compiler default constructor does some intelligent work like setting all the memory for the objects to zero, but it is not so." My doubt is I used to think the compiler default constructor provides initialization like it will set all the data members to zero I mean if they are of integer value. Then what exactly it does?
Jun 17, 2014 at 9:19am
Then what exactly it does?
All available default constructors of the member variables are called.

The author is right: The default constructor of primitive (or POD = plain old data) types will not be called.

Jun 17, 2014 at 9:19am
it initialises the variables yes, but you can't guarantee to what values, which is why you should always initialise them yourself to known values.
Jun 17, 2014 at 11:54am
then what good does it do? I mean it's like providing them with some garbage value by not initializing them
Jun 17, 2014 at 12:13pm
It is good especially for larger objects, which are going to be initialized with useful values before use anyway. For example, if you allocated an array with a very large number of elements, wouldn't it be much more efficient to simply set the values to the useful data (ignoring the old garbage values) rather than zeroing it out and then setting the data (ignoring the 0's)?
Topic archived. No new replies allowed.