C ++/CLI:从一个CLR类,多个C ​​++类继承 [英] C++/CLI: inherit from one CLR class, multiple C++ classes

查看:127
本文介绍了C ++/CLI:从一个CLR类,多个C ​​++类继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++/CLI中,我想要一个类似于以下内容的类层次结构:

In C++/CLI, I want a class hierarchy similar to the following:

Foo
  FooA : Foo, ClrClassA
  FooB : Foo, ClrClassB

FooA是否有可能共享一个(非CLR)基类,同时还继承自单独的CLR类?如果没有,那么FooA和FooB共享通用代码的最佳方法是什么?

Is it possible for FooA to share a (non CLR) base class while also inheriting from separate CLR classes? If not, what would be the best way for FooA and FooB to share common code?

推荐答案

通常来说,组成通常是比继承更好,因为它倾向于导致紧密耦合的设计.

Generally speaking, composition is often better than inheritance as it tends to lead to less tightly coupled designs.

如果您要混合使用托管代码和非托管代码,根据我的经验,通常将非托管代码包装在托管代码中要容易得多,反之亦然.

If you're mixing managed and unmanaged code, it's generally easier in my experience to wrap unmanaged code in managed code rather than visa versa.

托管代码不支持多重继承,并且有关Wikipedia的文章解释了为什么:

Multiple inheritance isn't supported for managed code and there's an article on Wikipedia which explains why:

像Visual C ++一样,托管C ++以及类和基于类的对象的使用仍然很普遍.托管C ++中对此的唯一主要更改是不支持多重继承的功能.这是由于CLR的限制.在CLR的垃圾回收器下管理的类不能继承多个类

Managed C++ and the use of classes and class based objects remains prevalent like in Visual C++. The only major change to this in Managed C++ is that the capabilities of multiple inheritance are not supported. This is because of a limitation of the CLR. A class managed under the CLR's garbage collector cannot inherit more than one class

在不知道为什么要合并类的情况下,很难给出关于如何最好合并类/功能的好答案...

It's difficult to give a good answer as to how best to combine your classes / functionality without knowing why you want to combine the classes...

这篇关于C ++/CLI:从一个CLR类,多个C ​​++类继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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