phonerec() : name("Me"),phonenunber("555-1000") {}An initializer can also call a function.
string getString() {Use this with care as the order of execution of the initializer list is not the order of that that list but the order that members occur in the class. You should write the initializer list in the order that the members occur in the class, otherwise you risk using uninitialized member variables.
return "value";
}
phonerec() : name(getString()),phonenunber("555-1000") {}
The example below shows the order that members are initialized. The initializer list is d, e, but the output is e d, exactly as the member variables are declared.
#include <iostream>
using namespace std;
int id() {
cout << "d" << endl;
return 1;
}
int ie() {
cout << "e" << endl;
return 1;
}
class a {
public:
int e;
int d;
a() : d(id()), e(ie()) { }
};
void main() {
a A;
}
On the next page : Initializing an array

