C++/CLI: inherit from one CLR class, multiple C++ classes

偶尔善良 提交于 2019-12-23 16:26:53

问题


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

Foo
  FooA : Foo, ClrClassA
  FooB : Foo, ClrClassB

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?


回答1:


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.

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

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...




回答2:


You cannot have a class inherit from both a reference and native type.



来源:https://stackoverflow.com/questions/6258158/c-cli-inherit-from-one-clr-class-multiple-c-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!