HashMap:处理托管对象C ++ [英] HashMap : Dealing with Managed objects C++
问题描述
我想这是一个愚蠢的问题,但这里是我的问题:
我想有一个 hash_map< int,Object ^> ;
作为我的对象 BigObject
的属性,它是用托管C ++编写的。
因此我必须声明一个指针 hash_map
如何插入对象? hash_map []
不能使用指针,我不能使插入工作(我不能使用 std :: pair< int,Object
您应该将hashmap声明为 hash_map< int,gcroot< Object ^>>
,您需要 #include< vcclr.h>
另请参阅 msdn
p>
#include< iostream>
#include< vcclr.h>
#include< hash_map>
使用命名空间std;
使用命名空间stdext;
使用命名空间system;
int main()
{
hash_map< ; int,gcroot< Object ^> hash;
hash.insert(make_pair gcnew String )));
return 0;
}
I guess it's kind of a stupid question but here is my problem :
I want to have a hash_map<int, Object^>
as an attribute of my object BigObject
, which is written in managed C++.
So I have to declare a pointer, hash_map<int, Object^>* hash
because I cannot declare explicitely native object in managed code.
How can I insert an object ? the hash_map[]
won't work with a pointer, and I cannot make insert work (I cannot use a std::pair<int, Object^>
because Object is managed...
Thanks a lot
You should declare your hashmap as hash_map<int, gcroot<Object^> >
. You will need to #include <vcclr.h>
See also msdn
edit: added code sample
#include <iostream>
#include <vcclr.h>
#include <hash_map>
using namespace std;
using namespace stdext;
using namespace System;
int main()
{
hash_map<int, gcroot<Object^> > hash;
hash.insert( make_pair<int, gcroot<Object^> >( 5,
gcnew String("hello world") ) );
return 0;
}
这篇关于HashMap:处理托管对象C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!