我不明白为什么在下面的代码中,当我实例化一个类型的对象时daughter,默认的grandmother()构造函数被调用?
我认为grandmother(int)应该调用构造函数(遵循我的mother类构造函数的规范),或者由于虚拟继承,这个代码根本不应该编译.
这里编译器grandmother在我的后面静默调用默认构造函数,而我从来没有要求它.
#include <iostream>
class grandmother {
public:
grandmother() {
std::cout << "grandmother (default)" << std::endl;
}
grandmother(int attr) {
std::cout << "grandmother: " << attr << std::endl;
}
};
class mother: virtual public grandmother {
public:
mother(int attr) : grandmother(attr) {
std::cout << "mother: " << attr << std::endl;
}
};
class daughter: virtual public mother {
public:
daughter(int attr) : mother(attr) {
std::cout << "daughter: " << …Run Code Online (Sandbox Code Playgroud)