开发者

Establish a mapping between a managed object and a native pointer in c++/cli?

开发者 https://www.devze.com 2023-03-25 04:41 出处:网络
I have a c++/cli class in which I would like to maintain a mapping between a managed string and a native pointer.

I have a c++/cli class in which I would like to maintain a mapping between a managed string and a native pointer.

Using std::map gives the compiler Warning C4368 (cannot define 'member' as 开发者_开发技巧a member of managed 'type': mixed types are not supported).

Using Dictionary gives C3225: generic type argument for 'TValue' cannot be 'native pointer', it must be a value type or a handle to a reference type

How can I achieve this mapping?


Just make a value type which holds the native pointer, i.e.

value struct TValue { native* ptr; };

Dictionary<String^, TValue> d;


Dictionary<String^, IntPtr> is your best bet. Unfortunately, IntPtr is conceptually equivalent to void*, so you lose type information and will have to cast the value to the real pointer type every time you want to use it.

0

精彩评论

暂无评论...
验证码 换一张
取 消