CSDN博客

img tang1975

Add reference count capability to your class with a template class

发表于2002/4/22 9:32:00  366人阅读

Because my system can't install Chinese IME, so... In fact, everyone read this article should know my english is very pool.

// This template class is used to add "ref count" feature for any class
template <class T>
class CRefObject : public T
{
public:
 DWORD AddRef(void)
 {
  return ::InterlockedIncrement((LPLONG)&m_dwRef);
 }
 DWORD Release(void)
 {
  if (0 == m_dwRef ||
   0 == ::InterlockedDecrement((LPLONG)&m_dwRef))
  {
   delete this;
   return 0;
  }
  return m_dwRef;
 }
 // Create an instance buf ref == 0
 static CRefObject<T> * CreateInstance(void)
 {
  CRefObject<T> * p = NULL;
  p = new CRefObject<T>();
  return p;
 }
protected:
 // Note: All T MUST have a default constructor
 CRefObject(void) : m_dwRef(0)
 {
 }
 // I think it is unnecessary to define a "virtual" distrcutor
 ~CRefObject() {};
 DWORD m_dwRef;
};

 

阅读全文
0 0

相关文章推荐

img
取 消
img