I am trying to write my own implementation of unique_ptr in C++, and here is my UniquePtr header file:
#include <iostream>
template <typename T>
class UniquePtr
{
T* ptr;
public:
UniquePtr(T* _ptr);
UniquePtr(const UniquePtr& other) = delete;
UniquePtr& operator=(const UniquePtr& other) = delete;
UniquePtr(UniquePtr&& dying) noexcept;
UniquePtr& operator=(UniquePtr&& dying) noexcept;
T& operator*();
const T& operator*() const;
/*??? operator->()
{
}*/
/*??? operator->() const
{
}*/
~UniquePtr();
};
I am confused about what operator-> should return. I know that a call in the form uniquePtr->member is translated to (*uniquePtr).member, but this still does not help me understand what the operator-> should return.
I understand that operators are basically functions with a special name, so we can think of -> as a function that takes just one argument - a pointer to the object (which all member functions get). However, I am a little confused about how the translation of the call to uniquePtr->member should dictate the way in which we should override it in the class (what its return type should be).
Could someone explain how to implement operator-> for this custom UniquePtr class and why it is implemented that way?
This does not answer my question:
What are the basic rules and idioms for operator overloading?
operator->
in that page you'll find a short paragraph that explains how overloadingoperator->
works. It even uses an example of pointer-like type which says you should returnvalue_type*
, just the same as your case.