Wie lautet die Reihenfolge des Konstruktoraufrufs bei der virtuellen Vererbung in C++?Wie lautet die Reihenfolge des Konstruktoraufrufs in der virtuellen Vererbung?
Für die folgenden zwei Fälle der Mehrfachvererbung;
(I) für den folgenden Code, ohne virtuelle Vererbung;
class a
{
public:
a()
{
cout<<"\t a";
}
};
class b: public a
{
public:
b()
{
cout<<"\t b";
}
};
class c: public b
{
public:
c()
{
cout<<"\t c";
}
};
class d: public c
{
public:
d()
{
cout<<"\t d";
}
};
class e: public c, public d
{
public:
e()
{
cout<<"\t e";
}
};
class f: public b, public e
{
public:
f()
{
cout<<"\t f";
}
};
int main()
{
f aaa;
return 0;
}
Die Ausgabe lautet:
a b a b c a b c d e f
(II) Mit virtueller Vererbung der Klasse E:
class a
{
public:
a()
{
cout<<"\t a";
}
};
class b: public a
{
public:
b()
{
cout<<"\t b";
}
};
class c: public b
{
public:
c()
{
cout<<"\t c";
}
};
class d: public c
{
public:
d()
{
cout<<"\t d";
}
};
class e: public c, public d
{
public:
e()
{
cout<<"\t e";
}
};
class f: public b, public virtual e
{
public:
f()
{
cout<<"\t f";
}
};
int main()
{
f aaa;
return 0;
}
Die Ausgabe lautet:
a b c a b c d e a b f
Kann jemand erklären, wie die Ausgabe wird in beiden Fällen erhalten? Wie wirkt sich virtuelle Vererbung auf die Konstruktion von Objekten aus?