Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Default Constructors and initialization of members of class

Reply
Thread Tools

Default Constructors and initialization of members of class

 
 
Pallav singh
Guest
Posts: n/a
 
      12-06-2010
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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      12-06-2010
On 12/ 7/10 08:05 AM, Pallav singh wrote:
> 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.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Pallav singh
Guest
Posts: n/a
 
      12-06-2010
On Dec 7, 12:25*am, Ian Collins <(E-Mail Removed)> wrote:
> On 12/ 7/10 08:05 AM, Pallav singh wrote:
>
>
>
> > 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.
>
> --
> Ian Collins


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

IS MY "a" getting initialized here ?

++++++++++++++++++++++++++++++++++++++++
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-06-2010
On 12/6/2010 2:55 PM, Pallav singh wrote:
> On Dec 7, 12:25 am, Ian Collins<(E-Mail Removed)> wrote:
>> On 12/ 7/10 08:05 AM, Pallav singh wrote:
>>
>>
>>
>>> 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.
>>
>> --
>> Ian Collins

>
> ++++++++++++++++++++++++++++++++++++++++
>
> 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
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      12-07-2010
On Dec 6, 8:02 pm, Victor Bazarov <(E-Mail Removed)> wrote:
> On 12/6/2010 2:55 PM, Pallav singh wrote:
> > On Dec 7, 12:25 am, Ian Collins<(E-Mail Removed)> wrote:
> >> On 12/ 7/10 08:05 AM, Pallav singh wrote:


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


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


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.

--
James Kanze
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
value & default initialization, and copy initialization Taras_96 C++ 3 10-30-2009 10:51 AM
Is the possible to have all the public constructors of the publicbase class as the constructors of a derived class? Peng Yu C++ 5 09-19-2008 10:19 AM
what's the difference between value-initialization and default-initialization? Jess C++ 23 05-04-2007 03:03 AM
Can nested class members access private members of nesting class? CoolPint C++ 8 12-14-2003 02:30 PM
Constructors and class members Filip Ruszkowski C++ 6 07-25-2003 02:18 PM



Advertisments