Sequence of same rank constructor call

Discussion in 'C++' started by francesco.orsenigo@gmail.com, Aug 15, 2006.

  1. Guest

    In the code below, at instantiation of a "listC" object, the
    constructors for attributes "a", "b", and "c" are called in sequence
    from "a" to "c".
    How much is this behaviour reliable?
    Does the c++ specifics tell something about it?
    What are the odds for the constructors to be called in a different
    sequence, like "cba" or "bca" or whatever?
    What if "a", "b" and "c" are not of the same class?

    Thanks,
    Francesco



    #include <iostream>

    class itemC { public:
    int item;
    itemC() { std::cout << "init object at " << (void*)this <<
    std::endl; }
    };

    class listC { public:
    itemC a;
    itemC b;
    itemC c;
    };

    int main()
    {
    listC list;
    }

    //EOF
     
    , Aug 15, 2006
    #1
    1. Advertising

  2. mlimber Guest

    wrote:
    > In the code below, at instantiation of a "listC" object, the
    > constructors for attributes "a", "b", and "c" are called in sequence
    > from "a" to "c".
    > How much is this behaviour reliable?
    > Does the c++ specifics tell something about it?
    > What are the odds for the constructors to be called in a different
    > sequence, like "cba" or "bca" or whatever?
    > What if "a", "b" and "c" are not of the same class?
    >
    > Thanks,
    > Francesco
    >
    >
    >
    > #include <iostream>
    >
    > class itemC { public:
    > int item;
    > itemC() { std::cout << "init object at " << (void*)this <<
    > std::endl; }
    > };
    >
    > class listC { public:
    > itemC a;
    > itemC b;
    > itemC c;
    > };
    >
    > int main()
    > {
    > listC list;
    > }
    >
    > //EOF


    The constructors are guaranteed to be called in the order of member
    declaration. Even if you put their constructors in a different order in
    your initialization list, they will still be called in the order of
    member declaration.

    Cheers! --M
     
    mlimber, Aug 15, 2006
    #2
    1. Advertising

  3. Guest

    Wow, that was fast!
    Thanks man, this saves me a lot of troubles!!!!

    Cheers,
    Francesco

    > The constructors are guaranteed to be called in the order of member
    > declaration. Even if you put their constructors in a different order in
    > your initialization list, they will still be called in the order of
    > member declaration.
    >
    > Cheers! --M
     
    , Aug 15, 2006
    #3
    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. Matt Graham
    Replies:
    3
    Views:
    526
    Matt Graham
    Feb 7, 2004
  2. lallous

    Constructor call constructor

    lallous, Mar 5, 2004, in forum: C++
    Replies:
    3
    Views:
    36,979
    Chris \( Val \)
    Mar 5, 2004
  3. Replies:
    10
    Views:
    631
    msalters
    May 9, 2005
  4. Replies:
    9
    Views:
    987
    Alf P. Steinbach
    Mar 6, 2006
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,319
Loading...

Share This Page