Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Diamond inheritance in C++

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 296

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report