我必須在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) –