Nie rozumiem, dlaczego w poniższym kodzie, gdy zainicjować obiekt typu daughter
, domyślny konstruktor grandmother()
jest wywoływana?Dlaczego wywoływany jest domyślny konstruktor w dziedziczeniu wirtualnym?
Pomyślałem, że należy wywołać konstruktora grandmother(int)
(w celu dostosowania do specyfikacji mojego konstruktora klasy mother
), lub ten kod nie powinien się w ogóle kompilować z powodu wirtualnego dziedziczenia.
Tutaj kompilator po cichu wywołuje domyślnego konstruktora, a ja nigdy o to nie prosiłem.
#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: " << attr << std::endl;
}
};
int main() {
daughter x(0);
}
Jaki kompilator (i wersja)? Z jakimi argumentami go skompilowałeś? – orlp
gcc 4.6.3 20120306 (Red Hat 4.6.3-2) na Fedorze 15. Argumentami są: -O0 -g3 -Wall -c -fmessage-length = 0 –
g ++ 4.1.2 ma ten sam problem: http: // codepad.org/L0jBXfSP – orlp