arun wrote:
[...] it appears that if
a class/template has a default constructor and it is a data member in
another class, then that default
constructor will be called regard less when you call the container
class constructor and you don't have
to do the following
B has a data member C then you don't have to do this explicitely
B() : C() //
and following will work fine.
B() {// code goes here }
If this is the case then why does not the compiler initializes
primitive data members like ints to 'zero'
values.
Because the language Standard says so.
When I don't explicitely initialize ints then their values
after the constructor is called is
definitely not int but some thing else.
Primitive data types are left uninitialised for optimization. If you
don't care for a member of a primitive type to be initialised to
anything, then the compiler is not the one to decide what to set it to.
Classes that do have constructors must have their constructors invoked.
That's just how class objects are created. Primitive types do not have
constructors and therefore don't need to be initialised to anything.
It's essentially the same behaviour that you'd get if you write
int i;
versus
someclass obj;
In the former case, 'i' is left uninitialised, whereas 'obj' is actually
_constructed_ by invoking the [default] constructor from 'someclass'.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask