如何实现C ++的多重继承? [英] How is C++'s multiple inheritance implemented?

查看:124
本文介绍了如何实现C ++的多重继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单继承容易实现。例如,在C中,继承可以被模拟为:

  struct Base {int a; } 
struct Descendant {Base parent; int b; }

但是使用多重继承,编译器必须在新构造的类中安排多个父节点。



我看到的问题是:如果父母是以AB或BA,或者甚至是其他方式安排的?然后,如果我做一个演员:

  SecondBase * base =(SecondBase *)& object_with_base1_and_base2_parents; 

编译器必须考虑是否更改原始指针。

解决方案

来自C ++的创建者的以下文章描述了多重继承的可能实现: / p>

C ++的多重继承

a> -Bjarne Stroustrup


Single inheritance is easy to implement. For example, in C, the inheritance can be simulated as:

struct Base { int a; }
struct Descendant { Base parent; int b; }

But with multiple inheritance, the compiler has to arrange multiple parents inside newly constructed class. How is it done?

The problem I see arising is: should the parents be arranged in AB or BA, or maybe even other way? And then, if I do a cast:

SecondBase * base = (SecondBase *) &object_with_base1_and_base2_parents;

The compiler must consider whether to alter or not the original pointer. Similar tricky things are required with virtuals.

解决方案

The following paper from the creator of C++ describes a possible implementation of multiple inheritance:

Multiple Inheritance for C++ - Bjarne Stroustrup

这篇关于如何实现C ++的多重继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆