Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

does the default constructor initialize values?

0 views
Skip to first unread message

LearningC++

unread,
Jul 20, 2006, 11:38:12 AM7/20/06
to
does the default constructor initialize values?

I have a class as defined below:

class A
{

int i;
char c;
int * iPtr;

};


Does the default constructor initialize the values of i, c & iPtr?

If so, what are the values that they each get initialized to?

thanks.

Ulrich Eckhardt

unread,
Jul 20, 2006, 11:47:43 AM7/20/06
to
LearningC++ wrote:
> class A
> {
> int i;
> char c;
> int * iPtr;
> };
>
>
> Does the default constructor initialize the values of i, c & iPtr?

No.

Uli

--
FAQ: http://ma.rtij.nl/acllc-c++.FAQ.html

Anand Hariharan

unread,
Jul 20, 2006, 11:59:45 AM7/20/06
to

There are two kinds of initialisation - default initialisation and
value initialisation.

Taking your example, if one defined an object as -

A DefaultInit;

- then the members of 'DefaultInit' are default initialised. Since
they are the basic types, the value each will have is undefined.

If instead, one defined an object as -

A ValueInit = A();

- then the members of 'ValueInit' are Value initialised, i.e., the
members will each be initialised to 0, '\0' and NULL respectively.

Since, as the class author, you want control how your members are
initialised irrespective of how the client users of your class create
objects, you would want to define your own constructor rather than let
the compiler synthesise one for you.

- Anand

LearningC++

unread,
Jul 20, 2006, 3:27:39 PM7/20/06
to
Thanks Anand. That is clear now.

Salt_Peter

unread,
Jul 21, 2006, 10:33:22 PM7/21/06
to

no, the default ctor does no relevent initialization since whatever
values are already present are garbage values. Residual garbage if you
prefer. Unfortunately, some still beleive that to be valid
initialization.

In no way does that prevent you from taking on the honourable
responsability of providing your own def ctor:

class A
{
int i;
char c;
int * iPtr;

public:
A() : i(0), c('a'), iPtr(0) { }
~A() { }
};

That default ctor now initializes any default instance of A including
those found in a primitive container - which, by the way, require a
default ctor (compiler generated or not).

int main()
{
A array[10]; // all elements now have valid componants
}

That is, if you wrote the class as follows:

class A
{
int i;
char c;
int * iPtr;

public:
A(int n, char t, int* ptr) : i(n), c(t), iPtr(ptr) { }
~A() { }
};

..the compiler would be incapable of constructing the elements of the
array as is.

LearningC++

unread,
Jul 23, 2006, 10:46:16 PM7/23/06
to
Salt_Peter:

Anand saying that if I use in the follwoing way the values get
initialized.

A ValueInit = A();

- then the members of 'ValueInit' are Value initialised,
i.e., the
members will each be initialised to 0, '\0' and NULL
respectively.


I now double checked online FAQS and found that he is correct about
that....


when I just use "A a;" then object a does not get initialized. I think
you are explaining for this scenario.. right...

Thanks.

0 new messages