Ich habe versucht, viel zu finden, dass was ist, wenn nur eine Klasse virtuell in mehrfacher Vererbung gemacht wird? Das Verhalten des Konstruktoraufrufs ist mir in diesem Fall nicht klar. zum Beispiel sagen wir code-Sequenz von Konstruktoraufrufen in mehrfacher Vererbung
#include<iostream>
using namespace std;
class grand{
public:
grand(){cout<<"grandfather"<<endl;}
};
class parent1:virtual public grand{ //virtual used only here
public:
parent1(){cout<<"parent1 "<<endl;}
};
class parent2: public grand{
public:
parent2(){cout<<"parent2"<<endl;}
};
class child:public parent1,public parent2{
public:
child(){cout<<"child"<<endl;}
};
int main() {
child s;
return 0;
}
Der Ausgang dieses Codes kommt als
grandfather
parent1
grandfather
parent2
child
aber in obigen Code, wenn wir diese
class parent1:virtual public grand{
public:
parent1(){cout<<"parent1 "<<endl;}
};
class parent2: public grand{
public:
parent2(){cout<<"parent2"<<endl;}
};
dieser
class parent1:public grand{ //virtual removed from here
public:
parent1(){cout<<"parent1 "<<endl;}
};
class parent2:virtual public grand{ //virtual is added here
public:
parent2(){cout<<"parent2"<<endl;}
};
ändern
Ausgabe wird angezeigt als
grandfather
grandfather //why parent1 constructor is not called here?
parent1
parent2
child
Mein Anliegen ist, warum parent1 Konstruktor nicht nach Großvater aufgerufen wird?
möglich Duplikat von [Virtual Inheritance, eine Klasse genug?] (Http://stackoverflow.com/questions/13752482/virtual-heritance-one-class-enugh) – Mikhail
Nein, das ist über * Reihenfolge der Anrufe * nicht über * Teilen * von selbst. –