-
Initilize data member
Here is the code:
class Base
{
public:
Base();
protected:
int m_data1;
private:
int m_data2;
};
Base::Base()
:m_data1(0),
m_data2(0)
{
}
class Derived : public Base
{
public:
struct score{
int courses;
int avg;
};
Derived();
private:
score m_score;
int m_data3;
};
Derived::Derived()
:Base(),
m_data3(0)
{
m_data1=10;
m_score.courses=0;
m_score.avg=0;
}
Question:
If in Derived::Derived(), initilize the data member as below:
Derived::Derived()
:Base(),
m_data1(10),
m_score.courses(0),
m_score.avg(0),
m_data3(0)
{
}
I got compiler errors. Anyone knows the rule here? m_data1, and m_score are derived class data member, why am I not allowed to initilize like this?
Thanks for reply.
-
the base constructor is automatically called when a var of type base is created there is no need to explicitly call it.
-
I think here I am trying to initilize m_data1 to 10, not 0. So it still need to assign value. Also, not sure why structure object could be use assignment initilization.
BTW, any body knows the advantage of assignment initilization than assignment in parentheses?
-
assignment in parens is supposed to be faster, but assignment initialization is easier to follow and understand.
-
If you mean the intialisation lists in class constructors as oposed to initialising the member variables in the body of the constructor, then if you're using non-built in types in your class then the initialisation list will be quicker.
When an instance of your class is created the member variables are created first before any code in the body in your constructor is executed. Therefore if you have nested classes as member variables then their default constructor will be called(any you'll be in trouble if you don't have a default constructor). Then when the body of your constructor is executed the assignment operator or copy constructor will be called to update these nested classes with the values that you have supplied in your constructor.
Using an initialisation list means that the appropriate nested class constructor can be called when these nested objects are created and prevents the extra assignment function/copy constructor.
-
Zen, if my understanding is correct, you are saying the following code is correct with better performance? Can u verify?
class Base
{
public:
Base();
protected:
int m_data1;
private:
int m_data2;
};
Base::Base()
:m_data1(0),
m_data2(0)
{
}
class Derived : public Base
{
public:
struct score{
int courses;
int avg;
score();
};
Derived();
private:
score m_score;
int m_data3;
};
Derived::score::score()
:courses(0),
avg(0)
{
}
Derived:Derived()
:Base(),
m_score(),
m_data3(0)
{
m_data1=10;
}
-
If all you are calling are default consructors and initialising built in types, I'm don't think that there would be any performance gain, but people still use the initialisation list out of force of habit regardless.
There is no point in calling default constructors that take no arguments explicitly in the initialisation list because it'll get called when the size object is created in the Derived class.