Mam sobie taki kod.
class Mother {
public:
Mother() {
cout << "Im mother" << endl;
}
~Mother() {
cout << "I died" << endl;
}
};
class Daugther : public Mother {
public:
Daugther(){
cout << "Im daughter" << endl;
}
~Daugther() {
cout << "ehh ";
}
};
int main() {
Mother* a = new Mother();
Mother* b = new Daugther();
Daugther* c = new Daugther();
delete a;
delete b;
delete c;
cin.get();
return 0;
}
Po uruchomieniu programu pojawia się taki wynik:
Im mother
Im mother
Im daughter
Im mother
Im daughter
I died
I died
ehh I died
Dla córki więc konstruktor uruchomił się za każdym razem poprawnie, bo mamy dwa Im daughter.
Dlaczego jednak destruktor dla córki uruchomił się tylko raz, skoro przecież mogę chyba użyć wskaźnika na klase matke jako wskaznik na córke?