参考模式计算地图元素? [英] Pattern for reference counted map elements?

查看:62
本文介绍了参考模式计算地图元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我想知道是否有一个已知模式用于创建

引用计数对象的地图,这样当最后一个对象的引用是

删除,引用该对象的元素将从地图中删除。


因为Boost的shared_ptr类允许自定义删除器被指定,我想是这样的事情:

std :: map< int,boost :: shared_ptr< SomeClass MyMap;


//向地图添加元素

MyMap [1] = boost :: shared_ptr< SomeClass>(new SomeClass,CustomDeleter);

其中''CustomDeleter''必须定义,以便它可以从地图中删除

对应的元素。我不确定这是否是最好的

解决方案,或者它是否可行。


我们将非常感谢所提供的任何见解。


- 丹尼斯

解决方案



" Dennis Jones" < no **** @ nospam.comwrote in message

news:IMqwh.4910


lk3.283@trnddc04 ...
< blockquote class =post_quotes>
其中''CustomDeleter''必须定义,以便它可以从地图中删除

对应的元素。我不确定这是否是最好的

解决方案,或者它是否可行。



它不会起作用,因为只要元素在地图中,它的引用

count将始终至少为1 。


所以我仍然需要一个解决方案。想法?


- 丹尼斯


2月1日上午11:49,Dennis Jones < nos ... @nospam.comwrote:


" Dennis Jones" < nos ... @ nospam.com写信息


新闻:IMqwh.4910


Hello,

I would like to know if there is a known pattern for creating a map of
reference-counted objects, such that when the last reference to an object is
deleted, the element referring to that object is removed from the map.

Since Boost''s shared_ptr class allows a custom deleter to be specified, I
was thinking about something like this:
std::map<int, boost::shared_ptr<SomeClass MyMap;

// add an element to the map
MyMap[ 1 ] = boost::shared_ptr<SomeClass>( new SomeClass, CustomDeleter );
Where ''CustomDeleter'' would have to defined such that it could remove the
corresponding element from the map. I''m not sure if this is the best
solution, or if it would even work.

Any insight offered will be appreciated.

- Dennis

解决方案


"Dennis Jones" <no****@nospam.comwrote in message
news:IMqwh.4910


lk3.283@trnddc04...

Where ''CustomDeleter'' would have to defined such that it could remove the
corresponding element from the map. I''m not sure if this is the best
solution, or if it would even work.

It won''t work because as long as the element is in the map, its reference
count will always be at least 1.

So I still need a solution. Ideas?

- Dennis


On Feb 1, 11:49 am, "Dennis Jones" <nos...@nospam.comwrote:

"Dennis Jones" <nos...@nospam.comwrote in message

news:IMqwh.4910


这篇关于参考模式计算地图元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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