Class object construction process
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Class object construction process

  1. #1
    Join Date
    Jan 2006
    Posts
    384

    Class object construction process

    Code:
    #include "stdafx.h"
    int func1()
    {
    	printf("Initializing i\n");
    	return 10;
    
    }
    int func2()
    {
    	printf("Initializing j\n");
    	return 20;
    }
    class A
    {
    private :
    
    	 int j;
    	 int i;
    public:
    	A():i(func1()),j(func2())
    	{
    	}
    
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	A a;
    	return 0;
    }
    What is it that causes func2() to be called before func1() in the above sample ? Is this as per the C++ specification ?
    Can you please outline the process of execution of the constructor and creation of data members during object construction ?
    Last edited by humble_learner; May 12th, 2010 at 04:26 AM.

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,699

    Re: Class object construction process

    Quote Originally Posted by humble_learner View Post
    What is it that causes func2() to be called before func1() in the above sample ?
    'j' is declared before 'i'
    Reverse the declaration order and you will see the opposite function order.
    This is defined behaviour.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  3. #3
    Join Date
    Aug 2009
    Location
    Romania->Felnac
    Posts
    48

    Re: Class object construction process

    The data members are constructed in order of declaration in the class definition, so j first and i second, and the syntax: A():i(func1()),j(func2()) does initialization in the same time with construction.

    This is the reason that construction like that is more efficient like: A() {i = funct1(); j = funct2()} // in this particular case you won't notice that, but if you have bigger objects instead of int, first constructor will be more efficient.
    Last edited by Zlatomir; May 12th, 2010 at 04:56 AM.

  4. #4
    Join Date
    Jan 2006
    Posts
    384

    Re: Class object construction process

    Thanks, but I wanted to understand why the same behaviour is not seen when the member variables are "assigned" in the constructor in the same order (on removing the initialization code)

    Just wanted to understand the relationship between the creation of member varibles, the triggering of initialization and assignment.

  5. #5
    Join Date
    Jan 2006
    Posts
    384

    Re: Class object construction process

    Quote Originally Posted by Zlatomir View Post
    The data members are constructed in order of declaration in the class definition, so j first and i second, and the syntax: A():i(func1()),j(func2()) does initialization in the same time of construction.
    OK. Thanks !

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,699

    Re: Class object construction process

    One thing to remember is to avoid constructing a variable in the initialiser list with the value of another. This can lead to subtle bugs if someone were to change the order of declaration.

    Code:
    class A
    {
    private :
    
         int i;
         int j;
    public:
        A():i(func1()),j(i) // Ok
        {
        }
    
    }; 
    
    class A
    {
    private :
    
         int j;
         int i;
    public:
        A():i(func1()),j(i) // Bug!!!
        {
        }
    
    };
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,281

    Re: Class object construction process

    Quote Originally Posted by humble_learner View Post
    Thanks, but I wanted to understand why the same behaviour is not seen when the member variables are "assigned" in the constructor in the same order (on removing the initialization code)

    Just wanted to understand the relationship between the creation of member varibles, the triggering of initialization and assignment.
    Construction qnd initialization are 2 very different processes. Construction is done by a very precise set of rules. For initialization, it's just your code, inside the constructor body, that is run.

    Note that:
    1 - Even for initialization the compiler may swap initialiation if it deams it will get better performance that way (less pipeline stalls come to mind).
    2 - When set to max warning or pedantic, GCC warnings, if not errors
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  8. #8
    Join Date
    Jan 2006
    Posts
    384

    Re: Class object construction process

    Quote Originally Posted by monarch_dodra View Post
    Construction qnd initialization are 2 very different processes. Construction is done by a very precise set of rules. For initialization, it's just your code, inside the constructor body, that is run.
    Hypothetically, If I were to look at the assembly code or machine code generated to look at the path traversed during a construction of an object, would the point at which member variables are initialized differ greately from a similar case where there is no initialization but member variables are assigned in the constructor body ?

  9. #9
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,281

    Re: Class object construction process

    Quote Originally Posted by humble_learner View Post
    Hypothetically, If I were to look at the assembly code or machine code generated to look at the path traversed during a construction of an object, would the point at which member variables are initialized differ greately from a similar case where there is no initialization but member variables are assigned in the constructor body ?
    Conceptually, you'll get the same difference as when you do:
    Code:
    void f1(int i, int j)
    {
        int a;
         int b;
        a = i;
        b = j;
        //do stuff with a & b
    }
    
    void f2(int i, int j)
     {
         int a = i;
          int b = j;
        //do stuff with a & b
     }
    However, the final result depends on how much your compiler optimizes. Also the little fact that un-initialized integers aren't technically constructed (but only allocated) also comes into account.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  10. #10
    Join Date
    Jan 2006
    Posts
    384

    Resolved Re: Class object construction process

    Thanks Monarch !

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Class object construction process

    gcc will give you a warning if you put variables in the initializer list in a different order than they're present in the class.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center