⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shared_ptr.h

📁 把html转成txt 把html转成txt
💻 H
字号:
//---------------------------------------------------------------------------

#ifndef shared_ptrH
#define shared_ptrH
//---------------------------------------------------------------------------
namespace DoxEngine
{
  template <class T> class shared_ptr
  {
    public:
      // Constructors
      shared_ptr()
{
  ptr = NULL;
  count = new int;
  *count = 1;
}


      shared_ptr(const shared_ptr<T> &original)
{
  ptr = original.ptr;
  count = original.count;
  (*count)++;
}

      shared_ptr(T* original)
{
  ptr = original;
  count = new int;
  *count = 1;
}

      ~shared_ptr()
{
  Release();
}


      //assignment
      shared_ptr<T> &operator=(const shared_ptr &rhs)
{
  if (this == &rhs)
    return *this;

  Release();
  ptr = rhs.ptr;
  count = rhs.count;
  Acquire();

  return *this;
}

      shared_ptr<T> &operator=(const T* rhs)
{
  if (ptr == rhs)
    return *this;

  Release();
  ptr = rhs;
  count = new int;
  *count = 1;
}


      //Use
      T* operator->()
{
  return ptr;
}


    private:
      T* ptr;
      int* count;

      void Acquire()
      {
        (*count)++;
      }
      
      void Release()
{
  (*count)--;
  if (!(*count))
  {
    delete count;

    if (ptr)
      delete ptr;

    ptr = NULL;
  }

}
  // Comparison
  bool operator>(const shared_ptr<T> &rhs)
  {
    return this>&rhs;
  }

  };
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -