我必须在CLI中包装一些本机类。
但我怀疑如何覆盖它们的包装中的虚拟方法。 因此,假设有一个虚拟的方法的本机类:“覆盖”托管包装中的本地虚拟方法的最佳方法
class NativeClass {
virtual void VMethod(std:string text) {
...
}
};
而且你想用一个管理类来包装它。我认为做这样的事情:
#pragma unmanaged
class NativeWrapper : public NativeClass {
public:
typedef void (*VMethodFunc)(std::string);
NativeWrapper(VMethodFunc VMethodFuncPtr)
: m_VMethodFuncPtr(VMethodFuncPtr) {}
void VMethod(std::string text) {
m_VMethodFuncPtr(text);
};
private:
VMethodFunc m_VMethodFuncPtr;
};
#pragma managed
ref class ManagedWrapper {
public:
// To Override
virtual void VMethod(String^ text) {
Console.WriteLine(text);
};
private:
void VMethod(std::string text) {
String^ sErr = gcnew String(text.c_str());
VMethod(sErr);
};
};
但我如何将ManagedWrapper :: VMethod(std :: string)绑定到VMethodFunc函数指针? 我在MSDN中发现了this article,但我认为它并不完全一样。
问候。
我不太明白你要在这里做什么。你想让ManagedWrapper提供一个简单地委托给NativeClass :: VMethod的虚拟VMethod(String ^文本)吗? –
使用构造函数创建NativeClass的实例,在析构函数和终结器中再次销毁它。 –
[C++/CLI混合模式DLL创建]的可能重复(http://stackoverflow.com/questions/2691325/c-cli-mixed-mode-dll-creation) –