与c++ /CLI和模板的协方差
Covariance with C++/CLI and Templates
我正在尝试在我的本地库和我的c#客户端代码之间建立一座桥梁。
为此,我有一个名为"IHasManagedWrapper"的接口:
#ifndef IHASMANAGEDWRAPPER_H_
#define IHASMANAGEDWRAPPER_H_
template <typename T>
class IHasManagedWrapper
{
public:
virtual T^ CreateManagedWrapper() = 0;
};
#endif
然后,为了测试返回的CLI类型的多态性,我创建了两个本机类,Parent和Child,其中Child继承自Parent:
父:#ifndef PARENT_H_
#define PARENT_H_
#include "IHasManagedWrapper.h"
ref class CLIParent;
class Parent : public IHasManagedWrapper<CLIParent>
{
public:
Parent();
~Parent();
virtual char* GetName();
virtual CLIParent^ CreateManagedWrapper();
};
#endif
孩子:
#ifndef CHILD_H_
#define CHILD_H_
#include "Parent.h"
#include "IHasManagedWrapper.h"
ref class CLIChild;
class Child : public Parent, IHasManagedWrapper<CLIChild> // uh-oh...
{
public:
char* GetName();
CLIChild^ CreateManagedWrapper();
}; // error C2555: 'Child::CreateManagedWrapper': overriding virtual function return type differs and is not covariant from 'Parent::CreateManagedWrapper'
#endif
我通过将"CLIChild"更改为"CLIParent"并从IHasManagedWrapper中删除CLIChild的继承来工作,但这意味着每次我调用Child->CreateManagedWrapper()时,我都会得到一个CLIParent对象,然后我需要手动转换为一个CLIChild对象。
。
CLIChild^ child = safe_cast<CLIChild^>(pChild->GetManagedWrapper()); // pChild->GetManagedWrapper() returns a CLIParent^ object
虽然这不是太坏,有一种方法,使它使CLIChild->CreateManagedWrapper()将返回一个CLIChild对象,同时保留IHasManagedWrapper接口?
谢谢!
您可以通过将重载虚方法的主体转移到特定方法(例如Child::CreateManagedWrapperChild
)中,然后在知道正在处理Child
时调用该特定方法来解决此问题。重载的虚方法只需调用特定的方法并将其结果上推到CLIParent
以匹配正确的方法签名。
class Child : public Parent {
public:
char* GetName();
CLIParent^ CreateManagedWrapper() { return CreateManagedWrapperChild(); }
CLIChild^ CreateManagedWrapperChild(); // actual code in this method
};
这看起来不像你希望写的那样干净,但它被编译器接受,并且在实践中应该只适用于一级继承。对于不止一个,您也必须使CreateManagedWrapperChild
虚,以同样的方式在GrandChild
中重载它,并且还要重载原始的虚拟方法以直接调用CreateManagedWrapperGrandChild
方法,以避免嵌套的虚拟调用。
然而,对于一个大的继承树,这种技术不是很实用,因为特定的虚拟方法爆炸了。
相关文章:
- 为什么是谷神星协方差.计算()似乎永远运行而不返回?
- 为什么需要返回指针来利用协方差?
- Eigen对修复非正定义的协方差矩阵有解吗
- 回调参数中的协方差C++
- 获取长双精度向量的方差
- 我在计算 4 个值的方差时的错误在哪里
- C++容器、协方差和模板
- "shared_ptr"如何实现协方差?
- C++协方差返回类型的缺点是什么
- 我遇到了一个关于多线程的小问题.需要多线程来计算 Pi 和方差
- 如何在犰狳中使用变量/方差函数
- 用c++计算平均值和方差
- 如何实现支持模板协方差的通用工厂
- C 协方差意外行为
- 练习:使用数组计算方差
- 带有指针返回问题的c++协方差问题
- 计算OpenCV中的协方差
- 使用 OpenCV 计算协方差矩阵
- C++类设计:协方差
- 逆方差的常见编程用法是什么?