|
发表于 2009-12-1 15:33:26
|
显示全部楼层
最近在玩template,送楼主一个好东东:- #include "ace/Atomic_Op.h
- template<class T> class Counted {
- static ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long> count;
- public:
- Counted() { ++count; }
- Counted(const Counted<T>&) { ++count; }
- static unsigned long getCount() { return count.value(); }
- protected:
- ~Counted() { --count; }
- };
- template<class T> ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long> Counted<T>::count(0);
复制代码
用法:
- class CountedClass : public Counted<CountedClass> {};
- class CountedClass2 : public Counted<CountedClass2> {};
复制代码
如果不用多线程的话,可把ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long>改成unsigned long
[ 本帖最后由 wishel 于 2009-12-1 15:35 编辑 ] |
|