现在的位置: 首页 > 综合 > 正文

C++ 属性 及 nullptr

2014年02月25日 ⁄ 综合 ⁄ 共 890字 ⁄ 字号 评论关闭

 

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;
   }
};

抱歉!评论已关闭.