我有一些 C++ 代码:
namespace Compute {
class __declspec(dllexport) IProgressCB {
public:
virtual void progress(int percentCompleted) = 0;
};
double __declspec(dllexport) compute(IProgressCB *progressCB, ...);
}
我需要从 C# 调用。
因此我想用 C++/CLI 包装这个 C++ 代码。
我了解如何包装 compute() 函数, 但是如何包装 IProgress 接口(interface)?
(.Net类好像不能继承C++类?)
最佳答案
使用一个 ref 类
,它持有一个指向包装实例的指针:
namespace ComputeCLI {
public ref class IProgressCB{
public:
void progress(int percentCompleted)
{
// call corresponding function of the wrapped object
m_wrappedObject->progress(percentCompleted);
}
internal:
// Create the wrapper and assign the wrapped object
IProgressCB(Compute::IProgressCB* wrappedObject)
: m_wrappedObject(wrappedObject){}
// The wrapped object
Compute::IProgressCB* m_wrappedObject;
};
public ref class StaticFunctions{
public:
static double compute(IProgressCB^ progressCB, ...){
Compute::compute(progressCB->m_wrappedObject, ...);
}
};
}
关于c# - 如何在 C++/CLI 中包装 C++ 接口(interface)(抽象类)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6489902/