Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mcleary/89921275cab7d4493a948260a2d910b4 to your computer and use it in GitHub Desktop.
Save mcleary/89921275cab7d4493a948260a2d910b4 to your computer and use it in GitHub Desktop.
Shows that the order in which constructor of classes with multiple inheritance is defined by the declaration in the header file.
#include <iostream>
using namespace std;
class BaseClass1
{
public:
BaseClass1()
{
cout << "Base class 1 constructor" << endl;
}
BaseClass1(int param)
{
cout << "Base class 1 constructor with param" << endl;
}
};
class BaseClass2
{
public:
BaseClass2()
{
cout << "Base class 2 constructor" << endl;
}
};
class DerivedClass : public BaseClass2, public BaseClass1
{
public:
DerivedClass() :
BaseClass1(10),
BaseClass2()
{
cout << "Derived class constructor" << endl;
}
};
int main()
{
DerivedClass dc;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment