Код: Выделить всё
#include
template
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 works
class Resource
{
public:
Resource() { std::cout sayHi() можно было бы ожидать, что все выражение ptr-> будет заменено необработанным указателем, но, очевидно, заменяется только ptr. Все, что следует далее, соответствует интуиции. Промежуточный шаг — вот что меня беспокоит. Является ли процесс перегрузки оператора стрелки исключительным случаем?
Это всего лишь теоретический вопрос.
Подробнее здесь: [url]https://stackoverflow.com/questions/79338503/in-c-is-the-overloading-of-the-arrow-operator-an-exceptional-case-or-does-it[/url]