Default Constructors and initialization of members of class

P

Pallav singh

Hi ,

I run following program in g++

#include<iostream>
using namespace std;

class A {
int * ptr1;
int * ptr2;
int a;
public:
void print()
{ cout << ptr1 << "\t" << ptr2 << "\t" << a <<endl; }
};

int main()
{
A a;
a.print();
return 0;
}

$ ./a.exe
0 0x22d000 1629643390

++++++++++++++++++++++++++++++++++++++++
IS MY i getting initialized here ?
++++++++++++++++++++++++++++++++++++++++

FROM C++ programming Language : Bjarne Stroustrup
10.4.2 Default Constructors [class.default]
A compiler generated default constructor implicitly calls the default
Constructors for a class’ members of class type and bases (§12.2.2).
For example:

Struct Tables {
int i;
int vi [10];
Table t1 ;
Table vt [10];
};

Tables tt ;

Here, tt will be initialized using a generated default constructor
that calls Table (15)
for tt.t1 and each element of tt.vt . On the other hand, tt.i and the
elements of tt.vi
are not initialized because those objects are not of a class type. The
reasons for
the dissimilar treatment of classes and builtin types are C
compatibility and fear
of runtime overhead.

Thx
Pallav Singh
 
I

Ian Collins

Hi ,

I run following program in g++

#include<iostream>
using namespace std;

class A {
int * ptr1;
int * ptr2;
int a;
public:
void print()
{ cout<< ptr1<< "\t"<< ptr2<< "\t"<< a<<endl; }
};

int main()
{
A a;
a.print();
return 0;
}

$ ./a.exe
0 0x22d000 1629643390

++++++++++++++++++++++++++++++++++++++++
IS MY i getting initialized here ?
++++++++++++++++++++++++++++++++++++++++

You don't have an i. Nothing is being initialised, try swapping the
order of the members and see what happens.
 
P

Pallav singh

You don't have an i.  Nothing is being initialised, try swapping the
order of the members and see what happens.

++++++++++++++++++++++++++++++++++++++++

IS MY "a" getting initialized here ?

++++++++++++++++++++++++++++++++++++++++
 
V

Victor Bazarov

++++++++++++++++++++++++++++++++++++++++

IS MY "a" getting initialized here ?

++++++++++++++++++++++++++++++++++++++++

Do you really need to ask redundant questions? Ian already told you
that *nothing* is being initialized. You didn't change the code since
your post, did you? Why would your correction make a difference?

Struct and class are different. You have a class, Stroustrup had a
struct. The difference is that the struct is an aggregate, and your
class (with private members) isn't.

What book are you reading that doesn't explain initialization? We need
to know to avoid recommending that book to beginners.

V
 
J

James Kanze

Do you really need to ask redundant questions? Ian already told you
that *nothing* is being initialized. You didn't change the code since
your post, did you? Why would your correction make a difference?
Struct and class are different.

Not really.
You have a class, Stroustrup had a struct. The difference is
that the struct is an aggregate, and your class (with private
members) isn't.

Had he put the public before the data members, that would have
made it an aggregate. Had Stroustrup provided a constructor,
that would have stopped his from being an aggregate. The choice
of keyword (struct or class) has no effect on whether something
is an aggregate.

More to the point here, whether the type is an aggregate has no
effect on the default initialization (unless the reason it's not
an aggregate is because there is a user defined constructor.
The difference between his code and Stroustrups, with regards to
initialization, is that Stroustrup declared the variable in
namespace scope (and thus, with static lifetime), where as the
poster declared it in block scope (and thus, with automatic
lifetime). All variables (aggregate or not) with static
lifetime are zero initialized before any code is executed.

(Except that I'm not sure what was exactly being quoted from
Stroustrup. He didn't indicate where the quote ended, but if it
included the text after the declaration, it sounds like
Stroustrup was describing exactly what he is seeing in his code:
the class type gets an implicit constructor, which is called,
but the implicit constructor only calls the constructors of the
members, and since int's and pointers don't have
constructors---or more formally, have trivial constructors,
which are no-ops---the int and pointer members of his class are
not initialized.)
What book are you reading that doesn't explain initialization?
We need to know to avoid recommending that book to beginners.

We first have to determine whether the book is wrong, or whether
he's just misinterpreting it. He's quoting from Stroustrup,
which very definitely does describe initialization, and
correctly. (But the book he's quoting from isn't really
designed for someone with no programming experience what so
ever. If that's his case, I'd very strongly recommend
Stroustrup's "Programming -- Priniciples and Practice Using
C++", which is one of the best introductory texts I've seen for
any language.
 

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

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top