免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3151 | 回复: 12
打印 上一主题 下一主题

请教一个STL的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-04-20 14:45 |只看该作者 |正序浏览
刚刚在测试list,auto_ptr的相关使用方法,但是代码编译通不过!请大家帮忙解决。
代码如下
  1. #include <list>;
  2. #include <vector>;
  3. #include <memory>;
  4. #include <iostream>;

  5. using namespace std;

  6. class test
  7. {
  8. public:
  9.         test( int k = 0 )
  10.         {
  11.                 i = k;
  12.                 cout << "test BBBB - " << i << endl;
  13.         }
  14.         ~test()
  15.         {
  16.                 cout << "test DDDD - " << i << endl;
  17.         }
  18.         test( const test& t )
  19.         {
  20.                 this->;i = t.i;
  21.         }
  22.         const test& operator=( const test& t )
  23.         {
  24.                 this->;i = t.i;
  25.                 return *this;
  26.         }

  27. private:
  28.         int i;
  29. };

  30. typedef auto_ptr<test>; test_ptr;
  31. typedef list<test_ptr>; test_list;

  32. int main()
  33. {
  34.         test_list tl;

  35.         for( int i = 0; i < 10; i++ )
  36.         {
  37.                 test_ptr ptr( new test( i ) );
  38.                 tl.push_back( ptr );
  39.         }
  40. }
复制代码

编译提示
/usr/include/g++-3/stl_construct.h: In function `void construct (_T1 *,
const _T2 & [with _T1 = test_ptr, _T2 = auto_ptr<test>;]':
/usr/include/g++-3/stl_list.h:293:   instantiated from `list<_Tp, _Alloc>;::_M_create_node (const _Tp & [with _Tp = test_ptr, _Alloc = allocator<test_ptr>;]'
/usr/include/g++-3/stl_list.h:344:   instantiated from `list<_Tp, _Alloc>;::insert (_List_iterator<_Tp, _Tp &, _Tp *>;, const _Tp & [with _Tp = test_ptr, _Alloc = allocator<test_ptr>;]'
/usr/include/g++-3/stl_list.h:381:   instantiated from `list<_Tp, _Alloc>;::push_back (const _Tp & [with _Tp = test_ptr, _Alloc = allocator<test_ptr>;]'
a.cpp:44:   instantiated from here
/usr/include/g++-3/stl_construct.h:48: passing `const auto_ptr<test>;'
as `this' argument of `auto_ptr<_Tp>;:perator
auto_ptr<_Tp>;::auto_ptr_ref<_Tp1>; () [with _Tp1 = test, _Tp = test]'
discards qualifiers

论坛徽章:
0
13 [报告]
发表于 2004-04-22 07:15 |只看该作者

请教一个STL的问题

这里有一些Smart Pointers的讨论,你可以看看
http://ootips.org/yonat/4dev/

论坛徽章:
0
12 [报告]
发表于 2004-04-22 01:19 |只看该作者

请教一个STL的问题

上面的拷贝构造函数漏了东西,应该如下
  1.     Smart_ptr(const Smart_ptr& rhs)
  2.     : m_pData(rhs.m_pData),
  3.     m_pCount(rhs.m_pCount)
  4.     {
  5.             m_pCount->;AtomicIncrement();
  6.     }
复制代码

论坛徽章:
0
11 [报告]
发表于 2004-04-22 01:16 |只看该作者

请教一个STL的问题

我的本意是对一个很大的类,比如Test,在与STL结合时希望避免拷贝对象的开销。由于auto_ptr不能配合STL使用,所以考虑效率和使用STL容器,我希望实现自己的智能指针提供这样的功能,这里可以提供实现思路(当然真正的实现还需要考虑更多的东西)
  1. class Smart_ptr
  2. {
  3. private:
  4.     // 计数器
  5.     // CAtomicCounter对象管理计数值,其AtomicIncrement递增计数值
  6.     // AtomicDecrement递减计数值并且在其值为0时返回true
  7.     // 在CAtomicCounter内部可以实现多线程的保护
  8.     mutable CAtomicCounter* m_pCount;
  9.     // 保存的对象指针
  10.     Test *m_pData;

  11. public:
  12.         Smart_ptr( Test *p )
  13.         : m_pData( p )
  14.         {
  15.         m_pCount = new CAtomicCounter( 1 );
  16.         }
  17.                
  18.     Smart_ptr(const Smart_ptr& rhs)
  19.     : m_pData(rhs.m_pData)
  20.     {
  21.             m_pCount->;AtomicIncrement();
  22.     }

  23.         ~Smart_ptr()
  24.         {
  25.                 if( m_pCount->;AtomicDecrement() )
  26.         {
  27.             delete m_pCount;
  28.                     m_pCount = NULL;
  29.                     delete m_pData;
  30.         }
  31.     }
  32. }
复制代码

论坛徽章:
0
10 [报告]
发表于 2004-04-21 23:46 |只看该作者

请教一个STL的问题

带引用计数的智能指针不是C++标准库中的吧。如果你自己实现,我觉得非常困难。增加引用计数非常容易,但是如何减少计数呢?我觉得这是关键,也是难点,你好好考虑考虑。

还有,要正确区分“重载”和“派生”的概念,你在上面有误用。

我说的是以 list 类为基类派生一个新的类,比如叫 pointer_list,专门用来处理元素是指针的情况,在它的析构函数中释放指针指向对象所占用的存储空间。

从你上面的帖子可以看出,你是想用指向 auto_ptr 的指针作为派生类的元素,完全没有这个必要,用普通的指针即可,如 pointer_list< test* >;,而不是 pointer_list< auto_ptr* >; 或者你的 pointer_list< SmartPtr* >;,这时使用它们没有任何的优越性,只能是一种负担。

论坛徽章:
0
9 [报告]
发表于 2004-04-21 12:39 |只看该作者

请教一个STL的问题

还有用自定义的适配STL的SmartPtr的方案有另一个好处,可以提供==,<运算符的重载以使用map,set等等容器,而用重载list<SmartPtr*>;的方案就不够通用了

论坛徽章:
0
8 [报告]
发表于 2004-04-21 12:36 |只看该作者

请教一个STL的问题

其实用whyglinux上面提到的重载list<SmartPtr*>;,在析构函数中对每个SmartPtr*调用delete的方法也可以,但是又增加了一个适配层,大家发表一下意见,到底哪种方案好呢?

论坛徽章:
0
7 [报告]
发表于 2004-04-21 12:33 |只看该作者

请教一个STL的问题

这个方案我也想到了,并且在一个项目中就是这么做的!但是它还是解决不了智能指针(带引用计数)使用标准模板库的问题,我的想法是在自己实现智能指针时,对于引用计数申明时增加mutable限定符,这样在拷贝时就可以对const SmartPtr&对象改变其引用计数,这样资源管理的自动化和标准模板库的强大特性都可以获得!
不知whyglinux对这个解决方案有何看法?

论坛徽章:
0
6 [报告]
发表于 2004-04-21 11:16 |只看该作者

请教一个STL的问题

以 list 类为基类派生一个新的类,在新类的析构函数中处理内存空间的回收问题。

论坛徽章:
0
5 [报告]
发表于 2004-04-21 08:54 |只看该作者

请教一个STL的问题

针对上面我们讨论的这个list与auto_ptr的问题!我想再跟大家继续讨论一下,对上面的例子程序而言,test是一个很小的类,实现的拷贝构造函数也就不存在效率问题,但是如果我的应用中实际是一个很大的类,那么频繁的复制操作就效率太低下了,而且对我得应用来说,生成一个test对象然后把它加入list中,然后有list来管理其生命周期是很合理的用法!同时我不想每次都要付出复制对象的代价!如果用list<test*>;的话又失去了对test对象资源管理的自动化。
大家有什么好的解决方案吗?可以在这里讨论讨论啊!同时希望whyglinux兄台继续发表高见
  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP