Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Diamond inheritance in C++

Shulou Source: shulou.com Published: 2022-06-03 19:26:11 09月15日 Update

This article mainly shows you the "example analysis of diamond inheritance in C++", which is easy to understand and well-organized. I hope it can help you solve your doubts. Now let the editor lead you to study and study the "example analysis of diamond inheritance in C++".

Diamond inheritance

Class Person {int _ AA;}; class Student:public Person {int _ BB;}; class Teacher: public Person {int _ CC;}; class Assistant: public Student, public Teacher {int _ DD;}

PS:

There are two Person members in the object of Assistant

Diamond inheritance has ambiguity and data redundancy.

Resolve:

Use virtual inheritance

First of all, when virtual inheritance is not used:

# include using namespace std; class AA {public: string _ aa;}; class BB: public AA {public: int _ bb;}; class CC: public AA {public: int _ cc;}; class DD: public BB, public CC {public: int _ dd;}; int main () {DD d; d.BB::_aa = 1; d.CC::_aa = 2; d._bb = 3; d._cc = 4; d._dd = 5; cout

Tags: Diamonds spaces examples caching analysis content objects articles offsets two locations models learning help ambiguity redundancy just addresses members data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Xiaomi Linux MariaDB NVidia