HashMap:处理托管对象C ++ [英] HashMap : Dealing with Managed objects C++

查看:139
本文介绍了HashMap:处理托管对象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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆