← Back to archive

Stdle #58 · virtual · 2026-08-03

What does this C++ snippet output?

A past puzzle — fully playable. 4 attempts, hints on wrong guesses.

#include <iostream>
#include <string>
#include <memory>
struct Base {
virtual std::string greet() { return "Hello from Base"; }
};
struct Derived : Base {
std::string greet() override { return "Hello from Derived"; }
};
int main() {
std::unique_ptr<Base> p = std::make_unique<Derived>();
std::cout << p->greet() << "\n";
}
Virtual
4 attempts left

Answer & explanation