0

I came across this c++ code that implements a ScopedPtr class as following:

#include <iostream>


class Shape {
    int x, y;
public:
    Shape() {
        std::cout << "Created shape\n";
    }
    Shape(int x, int y) : x(x), y(y) {
        std::cout << "Created non-default shape\n";
    }
    ~Shape() {
        std::cout << "Shape destroyed\n";
    }
    void print() { std::cout << "print\n"; }
};
template <typename T>
class ScopedPtr {
    T* ptr;
public:
    ScopedPtr(T* ptr) : ptr(ptr) {}
    ~ScopedPtr() { delete ptr; }
    T* operator->() { return ptr; }
};

int main() {
    ScopedPtr<Shape> shape = new Shape;
    shape->print();
}

The code works perfectly (i.e., Shape::print() is called) but that is my problem. The overloaded operator-> in the ScopedPtr class returns a pointer to the base type, but since print() was actually called then this call happened (shape->)->print(). Why exactly does this happen?.

  • 4
    [Built-in member access operators](https://en.cppreference.com/w/cpp/language/operator_member_access#Built-in_member_access_operators) is the odd-one - _"...If a user-defined `operator->` is called, `operator->` is __called again__ on the resulting value, recursively, until an `operator->` is reached that returns a plain pointer. After that, built-in semantics are applied to that pointer...."_ – Richard Critten May 25 '23 at 13:33

0 Answers0