我下面有此代码段,其中包含不同的运算符重载,而且要使它们正常工作非常困难(*,==和!=)。
template<typename T> class movable_ptr {
public:
//constructors and stuff
//...
//operators
T& operator*() const { return ptr_; };
T* operator->() const { return ptr_; };
explicit operator bool() const noexcept { return ptr_ != nullptr; };
bool operator!() const { return ptr_ == nullptr; };
bool operator==(const movable_ptr<T>& p) const { return p.get() == ptr_; };
bool operator!=(const movable_ptr<T>& p) const { return p.get() != ptr_; };
//access to variables
enable_movable_ptr<T>* get() {return ptr_; };
private:
enable_movable_ptr<T>* ptr_ = nullptr;
};
The trouble is, the compiler is unable to convert the ptr_
variable to the correct return type in operator*
. operator==
and operator!=
are in a similar situation, but this time it's the p.get()
the cannot be converted from const movable_ptr<T>
to movable_ptr<T> &
, so that it can be properly compared.
我敢肯定,我只是缺少一个小细节,但是我该如何解决呢?