Easy
Factory function returning a raw Shape pointer
What happens when this program runs?
main.cpp
struct Shape { virtual ~Shape() = default; virtual double area() const = 0; };
struct Circle : Shape { double area() const override { return 3.14; } };
Shape* createShape(const std::string& kind) {
if (kind == "circle") return new Circle();
return nullptr;
}
void useShape() {
Shape* s = createShape("circle");
if (s->area() > 1.0) {
std::cout << "big\n";
return; // early return -- s is never deleted
}
delete s;
}
int main() {
useShape();
}12 of 44 in Design Patterns