CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Initialization order in constructor

    Code:
    CMyClass
    {
    	Some_Type_1 obj1;
    	Some_Type_2 obj2;
    public:
    	CMyClass() : obj2(...), obj1(...){};
    };
    Standard says: Class members are initialized in the order of declaration. Fine.
    But, why is it not advisiable to do the above???
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

  2. #2
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Initialization order in constructor

    It's not advisable to do the above but it might be confusing. It will ignore the order in your initialiser list and initialise obj1 first anyway. Most of the time that won't make a difference but perhaps obj1 has a dependency on something you do when initialising obj2 (or on obj2 itself) which won't have happened yet.

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Initialization order in constructor

    While dealing with initialization lists you should always keep one thing in mind. According to the standard class members get initialized in the order they are declared not in the order they appear in the initialization list...
    Code:
    class CFoo
    {
    public:
      CFoo(int iValueOne, int iValueTwo);
    
    private:
      int m_iValueOne;
      int m_iValueTwo;
    };
    
    CFoo::CFoo(int iValueOne, int iValueTwo) : m_iValueTwo(iValueTwo), m_iValueOne(iValueOne) { ... }
    In this case 'm_iValueOne' gets initialized first although 'm_iValueTwo' is standing first within the initialization list. That standard behaviour can cause problems which are hard to find... To avoid this you should always use the same order at declaration and initialization time...

    This can result in severe problems if you want to initialize one member with another...

  4. #4
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Initialization order in constructor

    Quote Originally Posted by Vedam Shashank
    Standard says: Class members are initialized in the order of declaration. Fine.
    But, why is it not advisiable to do the above???
    I think it is also important to note, that class members are destroyed in the reverse order they are declared. Generally a good idea to destroy the members in the reverse order, they have been created (in case of dependencies).
    So if you could override the oder in which the class members are initialized (like you try in your example), the members would not get destroyed any more in the reverse order they have been created. That's why the standard fixes both the order of creation and destroying to the declaration, not to the implementation of any of the constructors.

  5. #5
    Join Date
    May 2004
    Location
    Michigan, United States
    Posts
    457

    Re: Initialization order in constructor

    Quote Originally Posted by treuss
    I think it is also important to note, that class members are destroyed in the reverse order they are declared. Generally a good idea to destroy the members in the reverse order, they have been created (in case of dependencies).
    So if you could override the oder in which the class members are initialized (like you try in your example), the members would not get destroyed any more in the reverse order they have been created. That's why the standard fixes both the order of creation and destroying to the declaration, not to the implementation of any of the constructors.
    The standard could have just as easily taken into account the order that the objects were initialized in the constructor initialization list and reversed that when destructing. I imagine the reason it doesn't is because you may have multiple constructors, each with a different initialization order. If that was the case, it would be impossible to know the order for destruction, unless it kept track of which constructor was used!
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  6. #6
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Thumbs up Re: Initialization order in constructor

    Quote Originally Posted by Bond
    The standard could have just as easily taken into account the order that the objects were initialized in the constructor initialization list and reversed that when destructing. I imagine the reason it doesn't is because you may have multiple constructors, each with a different initialization order. If that was the case, it would be impossible to know the order for destruction, unless it kept track of which constructor was used!
    It is even a little more complex, because even if there was only one constructor, the module containing the constructor definition may be different than the module containing the destructor definition, what implies that the compiler would be constrained to store this order in some static data exported (put in a read-only data section for example) from the constructor's module containing necessary informations on the order of construction.

  7. #7
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: Initialization order in constructor

    Thanks for the inputs.
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured