Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1236558
  • 博文数量: 389
  • 博客积分: 2874
  • 博客等级: 少校
  • 技术积分: 3577
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 10:34
文章分类

全部博文(389)

文章存档

2020年(2)

2018年(39)

2017年(27)

2016年(3)

2015年(55)

2014年(92)

2013年(54)

2012年(53)

2011年(64)

分类: C/C++

2012-03-12 14:05:41

    该模式引自游戏编程精粹,但是他那里面是windows的代码,放到linux下不能运行,所以进行了调整。  
    说明:在程序设计的时候,会经常用到一组或几组类型相同(class T)的数据,假设我们使用链表存储。
    面临的问题:
     1、链表存放的位置。
     2、每创建一个对象都要插入到该链表中,每销毁一个对象都要从链表中删除
     3、不能胡乱的插入和删除对象。
 
    要解决上面的问题,可能需要在很多地方插入类似的代码,往往这种代码就是引入bug的地方,有一种解决方案就是“自动列表模式”。

点击(此处)折叠或打开

  1. #ifndef _TAUTOLISTS_H_
  2. #define _TAUTOLISTS_H_

  3. #include <list>
  4. #include <algorithm>
  5. using namespace std;

  6. template <class T>
  7. class TAutolists
  8. {
  9.     typedef typename list<T*>::iterator iter_t;
  10. public:
  11.     TAutolists()
  12.     {
  13.         ms_List.push_front(static_cast<T*>(this));
  14.     }

  15.     virtual ~TAutolists()
  16.     {
  17.         ms_List.remove( static_cast<T*>(this));
  18.     }

  19. public:
  20.     static T* GetAutolistFirst()
  21.     {
  22.         if (ms_List.begin() == ms_List.end())
  23.         {
  24.             return NULL;
  25.         }
  26.         else
  27.         {
  28.             ms_ListIter = ms_List.begin ();
  29.             return *ms_ListIter;
  30.         }
  31.     }

  32.     static T* GetAutolistNext()
  33.     {
  34.         if (ms_ListIter == ms_List.end()
  35.             || ++ms_ListIter == ms_List.end())
  36.         {
  37.             return NULL;
  38.         }
  39.         else
  40.         {
  41.             return *ms_ListIter;
  42.         }
  43.     }

  44.     static unsigned int GetAutolistCount()
  45.     {
  46.         return ms_List.size ();
  47.     }
  48.         
  49. public:
  50.     static list<T*> ms_List;
  51.     static iter_t ms_ListIter;
  52. };


  53. // Declarations of static variables
  54. template <class T>
  55. list<T*> TAutolists<T>::ms_List;

  56. template <class T>
  57. //typedef typename list<T*>::iterator iter_t;
  58. typename list<T*>::iterator TAutolists<T>::ms_ListIter;

  59. #endif
测试程序:

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include "TAutolists.h"

  3. class vec : public TAutolists<vec>
  4. {
  5. public:
  6.     int m_x;
  7.     int m_y;
  8. } ;

  9. using namespace std;


  10. /*
  11. template <class T>
  12. class A
  13. {
  14. public:
  15.     static T a;
  16. };

  17. template <class T>
  18. T A<T>::a;
  19. int main (int argc, char *argv[])
  20. {
  21.     A<int>::a = 0;
  22. }
  23. */

  24. int main (int argc, char *argv[])
  25. {
  26.     vec lv1;
  27.     lv1.m_x = 1;
  28.     vec lv2;
  29.     lv2.m_x = 2;
  30.     cout << "count is " << lv1.GetAutolistCount() << endl;
  31.    
  32.     vec *cptmp = lv2.GetAutolistFirst();
  33.     while(cptmp)
  34.     {
  35.         cout << "x is " << cptmp->m_x << endl;
  36.         cptmp = lv2.GetAutolistNext();
  37.     }
  38.     return(0);
  39. }

IBM的c++
阅读(840) | 评论(0) | 转发(0) |
0

上一篇:iptables实现双向地址映射

下一篇:ssl隧道

给主人留下些什么吧!~~