Daleth,
thanks a lot. I think I understand it in part.
First, here is a part I understand best illustrated by the following example.
In the following code the constructor of B will be called twice, first the default one then the one with an integer parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#include <iostream>
using namespace std;
class B{
public:
B(){
cout << "B's empty constructor\n";
};
B(int k){
cout << "B's constructor with k=" << k << endl;
n = k;
};
int n;
};
class A {
public:
A(int k);
B b;
};
A::A(int k){b = *new B(k);}
int main(){
A a(3);
}
|
On the other hand, if I define the constructor A(int ) using initialization list as follows
I can eliminate the call to the default constructor B().
Here is the part I don't understand, yet.
Suppose the class A contained two or more objects that needed initialization, say B and C.
How can I use the initialization list to avoid using their default constructors?