Bryan wrote:
Hi,
Where is the proper place to use a member initialization list, the
header or cpp file?
The language syntax requires the init list to go with the definition of
the constructor.
>
Also, is there any difference between using a member initialization list
and initializaing member variables in the constructor?
i.e.
MyClass(void) : a(1), b(2) {};
Delete the semicolon. Delete void -- it's a C-ism.
>
vs
MyClass::MyClass(void)
Delete void -- It's a C-ism.
{
a = 1;
b = 2;
}
Yes. the first case directly constructs a and b. The second case
default constructs a and b, and then assigns them. If members are
expensive to construct and assign, you may see a performance hit (but
only worry about that *AFTER BENCHMARKING*), and the second case will
also fail if either a or b is a const member.