Thursday, September 24, 2015

Initialization list doesn't work for virtual constructor?

A virtual base class is always initialized before other derive class. This is a known behavior. I'm aware of it. But what's surprising me is that when I have an intermediate class pass parameter to the virtual base class constructor in their member initialization list, these initialization list will be ignored. The following piece could prove this statement.
class Parent {
 Parent() : param(0) {
  cout << "Parent constructor" << endl;

 Parent(int param) : param(param){
  cout << "Parent constructor(param)" << endl;

 int getParam() { return param; }

 int param;

class Base : virtual public Parent {
 Base() : Parent(5373) {
  cout << "Base constructor" << endl;

class Xtends : public Base {
 Xtends() {
  cout << "Xtends constructor" << endl;
When I execute the following piece, the param value would be 0:
   Xtends x;
   cout << "param: " << x.getParam() << endl;
Notice the Base class is an intermediate class. When I pass in the value of 5373 into Parent( int ) constructor, it simply ignores it. Next the Base class is no longer an intermediate class. Now when I execute the following code, the value of 5373 would be seen:
   Base b;
   cout << "param: " << b.getParam() << endl;

No comments: