C++ 属性 及 nullptr

来源:互联网 发布:魔兽世界数据库2.43 编辑:程序博客网 时间:2024/05/29 15:13

 

template<class T>
class CNullPtr
{
 T* m_ptr;

 T m_value;

 void SetValue(T value)
 {
  this->m_value = value;
  m_ptr = &m_value;
 }
 void SetValue(const CNullPtr& source)
 {
  if(source.m_ptr == nullptr)
  {
   this->m_ptr = nullptr;
  }
  else
  {
   SetValue(source.m_value);
  }
 }

public:
 CNullPtr():m_ptr(nullptr)
 {
 }
 CNullPtr(CNullPtr& obj)
 {
  SetValue(obj);
 }

   void putprop(T j) {
      SetValue(j);
   }
 
   T getprop() {
    if(this->m_ptr==nullptr)
    {
     throw "使用空值。";
    }

      return m_value;
   }
 
   __declspec(property(get = getprop, put = putprop)) T Value;//定义属性

   bool IsEmpty() {
    if(this->m_ptr==nullptr)
    {
     return true;
    }

    return false;
   }

   CNullPtr& operator=(const T& rhs)
   {
    SetValue(rhs);
    return *this;
   }

   CNullPtr& operator=(const CNullPtr<T>& rhs)
   {
    if (this == &rhs) return *this; // identity test: if a self-assignment,
    SetValue(rhs);
    return *this;
   }
  

   CNullPtr& operator=(std::nullptr_t rhs)
   {
    m_ptr = rhs;
    return *this;
   }
};