Response to: Constructors : What does this statement mean?
Revision 1
Most often it doesn't matter whether you use member initializer lists or assignments inside the body of your constructor. For example in your case you can perform the initialization of your int members from both the initializer list and the body of your constructor. However sometimes you can intialize a member only from the initializer list, and sometimes you HAVE TO initialize some members if you don't want to get a compile error!
class class_with_default_ctor
{
public:
class_with_default_ctor(int _i=0)
: i(_i)
{}
int i;
};
class class_without_default_ctor
{
public:
class_without_default_ctor(int _i)
: i(_i)
{}
int i;
};
class something
{
};
class test
{
public:
test(something& sg)
: m_class_with_default(5)
, m_class_without_default(6)
, m_const_int(7)
, m_const_something_ref(sg)
, m_something_ref(sg)
{
}
private:
class_with_default_ctor m_class_with_default;
class_without_default_ctor m_class_without_default;
const int m_const_int;
const something& m_const_something_ref;
something& m_something_ref;
};
Posted 4-Oct-12 13:27pm
by
pasztorpisti.