In multiple inheritance, I have a virtual Base
class which is inherited by class A
and class B
. A
and B
are base classes of AB
. Please see the code below.
In constructor of A
and B
, Base(string)
constructor is called. I am expecting to get following output:
Base::Base(std::string)
A::A()
B::B()
But I am getting following output:
Base::Base()
A::A()
B::B()
Why default constructor of Base
is being called?
#include<iostream>
#include<string>
using namespace std;
class Base{
public:
Base(){
cout<<__PRETTY_FUNCTION__<<endl;
}
Base(string n):name(n){
cout<<__PRETTY_FUNCTION__<<endl;
}
private:
string name;
};
class A : public virtual Base {
public:
A():Base("A"){
cout<<__PRETTY_FUNCTION__<<endl;
}
private:
string name;
};
class B : public virtual Base {
public:
B():Base("B"){
cout<<__PRETTY_FUNCTION__<<endl;
}
private:
string name;
};
class AB : public A, public B{
};
int main(){
AB a;
}