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 ?
Re: Class object construction process
Quote:
Originally Posted by
humble_learner
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.
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.
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.
Re: Class object construction process
Quote:
Originally Posted by
Zlatomir
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 !
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!!!
{
}
};
Re: Class object construction process
Quote:
Originally Posted by
humble_learner
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
Re: Class object construction process
Quote:
Originally Posted by
monarch_dodra
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 ?
Re: Class object construction process
Quote:
Originally Posted by
humble_learner
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.
Re: Class object construction process
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.