免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: xiaomiao
打印 上一主题 下一主题

使用class template扩展STL map容器类对象的一个例子 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-28 10:50 |显示全部楼层 |倒序浏览
//////////////////////////////////////////////////////////////////
//auto_map: 析购时候对map对象的second区域字段释放内存
/////////////////////////////////////////////////////////////////
template <class Key, class T, class Compare = less<Key>,
          class Allocator = allocator<pair<const Key, T> > >
class auto_map : public map<Key,T,Compare,Allocator>
{
public:
    explicit auto_map(const Compare& comp = Compare(),
             const Allocator& alloc = Allocator())
    :map<Key,T,Compare,Allocator>(comp,alloc)
    {
    };
    void clear()
    {
        fnFree();

        map<Key,T,Compare,Allocator>::clear();
    }
    ~auto_map()
    {
        fnFree();  
    };

private:
    void fnFree()
    {
            // you must declare typename keyword here
        typename map<Key,T,Compare,Allocator>::iterator it = map<Key,T,Compare,Allocator>::begin();
        for(;it != map<Key,T,Compare,Allocator>::end();++it)
        {
            delete (*it).second;
        }  
    }
};

论坛徽章:
0
2 [报告]
发表于 2006-10-01 14:44 |显示全部楼层
看了各位就此问题的深入讨论,受益非浅!我贴上的代码段来源自国内中兴通讯公司某项目的Source Code片断,本来觉得该公司C++高人很多,但看来有这么多隐含的小问题,请哪位高手写个更好的实现?用Shared_ptr要怎么做?多谢指教!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP