C++11 NullablePointer

1、一種類似於指針的類,可以和std::nullptr_t對象比較
2、滿足:
    相等比較(operator==)
    默認構造
    copy構造
    copy賦值
    析構
    可以作爲bool條件表達式使用。空值返回false,否則返回true
    不拋出異常

class nullPointer final
{
public:
	nullPointer(const std::nullptr_t vl = nullptr) noexcept :id(0) 
	{

	}

	nullPointer(const nullPointer& oth) noexcept
	{
		this->id = oth.id;
	}

	nullPointer& operator=(const nullPointer& oth) noexcept
	{
		this->id = oth.id;
		return *this;
	}

	~nullPointer() noexcept
	{

	}

	operator bool() noexcept
	{
		return this->id != 0;
	}

	bool operator!() noexcept
	{
		return this->id == 0;
	}

	friend bool operator==(const nullPointer lft, const nullPointer rgt) noexcept
	{
		return lft.id == rgt.id;
	}
	
	friend bool operator!=(const nullPointer lft, const nullPointer rgt) noexcept
	{
		return !(lft == rgt);
	}
private:
	int id;
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章