I have a question regarding the arrow operator's overload process. The overloading makes it seem like it is a unary operator, while the usage makes it appear as a binary operator. The return value isn't intuitive either. Only a raw pointer is returned.
#include <iostream>template <typename T>class Auto_ptr1{ T* m_ptr {};public: // Pass in a pointer to "own" via the constructor Auto_ptr1(T* ptr=nullptr) :m_ptr(ptr) { } // The destructor will make sure it gets deallocated ~Auto_ptr1() { delete m_ptr; } // Overload dereference and operator-> so we can use Auto_ptr1 like m_ptr. T& operator*() const { return *m_ptr; } T* operator->() const { return m_ptr; }};// A sample class to prove the above worksclass Resource{public: Resource() { std::cout << "Resource acquired\n"; } ~Resource() { std::cout << "Resource destroyed\n"; } void sayHi() { std::cout << "Hi!\n"; }};void someFunction(){ Auto_ptr1<Resource> ptr(new Resource()); // ptr now owns the Resource int x; std::cout << "Enter an integer: "; std::cin >> x; if (x == 0) return; // the function returns early // do stuff with ptr here ptr->sayHi();}int main(){ someFunction(); return 0;}
In the example ptr->sayHi(), one might expect the entire expression ptr-> to be replaced with the raw pointer, but apparently, only ptr is replaced. Anything that follows is coherent with intuition. The intermediate step is what bugs me. Is the overloading process of the arrow operator an exceptional case?
It's just a theory question.