does the default constructor initialize values?

N

NewToCPP

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.
 
N

Noah Roberts

NewToCPP said:
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?

It zero initializes them.
 
A

Alf P. Steinbach

* Noah Roberts:
It zero initializes them.

No, it does exactly nothing (in this class).

int main()
{
A o; // Default-constructed, members of o are not initialized.
}
 
R

Rolf Magnus

NewToCPP said:
does the default constructor initialize values?

That depends.
I have a class as defined below:

class A
{

int i;
char c;
int * iPtr;

};

Note that this class is quite useless, because all the members are private.
Does the default constructor initialize the values of i, c & iPtr?

In this case, the default constructor will be generated by the compiler. It
does nothing, so no, the values won't get initialized.
 
N

Noah Roberts

Alf said:
* Noah Roberts:

No, it does exactly nothing (in this class).

int main()
{
A o; // Default-constructed, members of o are not initialized.
}

That doesn't actually call the default constructor of a POD though.

A o = A();
 
F

Frederick Gotham

NewToCPP posted:
class A
{

int i;
char c;
int * iPtr;

};


That is equivalent to:

struct A {
private:

int i;
char c;
int *p;
};


Let's remove the "private" as it just complicates the example. So we have:

struct A {

int i;
char c;
int *p;
};

int main()
{
A obj1; /* Contains garbage */

A obj2 = {}; /* Each member gets its default value */

A obj3 = A(); /* Each member gets its default value */

A static obj4; /* Each member gets its default value */
}
 
A

Alf P. Steinbach

* Noah Roberts:
That doesn't actually call the default constructor of a POD though.

A o = A();

On the contrary. What you have here is copy construction from a
zero-initialized instance (the expression 'A()' means
default-initialization, 8.5/7, which for a POD is defined as
zero-initialization, 8.5/5). That's not to say that 'A o;' necessarily
actually calls a default constructor: it doesn't affect the outcome
whether you say that there is no default constructor for a POD class (no
call), or that the default constructor does absolutely nothing (call
does nothing), but the standard specifies an implicitly declared default
constructor, 12.1/5, which if it actually exists must do nothing at all.

In particular, if you derive from class A,

struct A { /* as before */ };

struct B: A { int x; int y; B(): x(1), y(2) {} };

B o;

then you have an object 'o' with x and y initialized, and i, c, and iPtr
uninitialized. If class A had a default constructor that did
zero-initialization, then instead you would have i, c and iPtr zeroed.
But that isn't what you get, so the notion of a zeroing automatically
generated default constructor leads to incorrect, dangerous conclusions.

However, with a /conforming/ compiler,

struct B: A { int x; int y; B(): x(1), y(2), A() {} };

should ensure zero-initializion of the A base class sub-object; for a
POD this syntax does not denote a call of the (possibly existing or not)
default constructor, it instead denotes default initialization which is
defined as zero-initialization (references given above).
 
N

NewToCPP

Does it initialize if the members are public?


Rolf said:
That depends.


Note that this class is quite useless, because all the members are private.


In this case, the default constructor will be generated by the compiler. It
does nothing, so no, the values won't get initialized.
 
N

NewToCPP

Frederick:

What are the default values for each of those members?

I guess for "i ==> 0", "iPtr ==> NULL" what is default for c?
 
F

Frederick Gotham

NewToCPP posted:
Frederick:

What are the default values for each of those members?

I guess for "i ==> 0", "iPtr ==> NULL" what is default for c?


Numeric types, e.g. integer types and floating point types, get 0.

("char" is a numeric type.)


"bool" becomes false.


Pointers get the null pointer value.


In all cases, it's as if you'd written:

IntrinsicType object = 0;
 
N

NewToCPP

What does it do in case of non-public members?


Frederick said:
NewToCPP posted:



Numeric types, e.g. integer types and floating point types, get 0.

("char" is a numeric type.)


"bool" becomes false.


Pointers get the null pointer value.


In all cases, it's as if you'd written:

IntrinsicType object = 0;
 
F

Frederick Gotham

NewToCPP posted:
What does it do in case of non-public members?


Everything's the same, regardless of whether a member is public, private or
protected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top