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?.