Default Constructors and initialization of members of class

Discussion in 'C++' started by Pallav singh, Dec 6, 2010.

  1. Pallav singh

    Pallav singh Guest

    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
     
    Pallav singh, Dec 6, 2010
    #1
    1. Advertising

  2. Pallav singh

    Ian Collins Guest

    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
     
    Ian Collins, Dec 6, 2010
    #2
    1. Advertising

  3. Pallav singh

    Pallav singh Guest

    On Dec 7, 12:25 am, Ian Collins <> 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 ?

    ++++++++++++++++++++++++++++++++++++++++
     
    Pallav singh, Dec 6, 2010
    #3
  4. On 12/6/2010 2:55 PM, Pallav singh wrote:
    > On Dec 7, 12:25 am, Ian Collins<> 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
     
    Victor Bazarov, Dec 6, 2010
    #4
  5. Pallav singh

    James Kanze Guest

    On Dec 6, 8:02 pm, Victor Bazarov <> wrote:
    > On 12/6/2010 2:55 PM, Pallav singh wrote:
    > > On Dec 7, 12:25 am, Ian Collins<> 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
     
    James Kanze, Dec 7, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Filip Ruszkowski

    Constructors and class members

    Filip Ruszkowski, Jul 24, 2003, in forum: C++
    Replies:
    6
    Views:
    420
    Ron Natalie
    Jul 25, 2003
  2. CoolPint
    Replies:
    8
    Views:
    989
    Jeff Schwab
    Dec 14, 2003
  3. Jess
    Replies:
    23
    Views:
    933
  4. Peng Yu
    Replies:
    5
    Views:
    399
    Juha Nieminen
    Sep 19, 2008
  5. Taras_96
    Replies:
    3
    Views:
    589
    Michael Tsang
    Oct 30, 2009
Loading...

Share This Page