← Back to archive

Stdle #71 · virtual · 2026-08-16

What does this C++ snippet output?

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

#include <iostream>
#include <string>
struct A { virtual ~A() {} virtual std::string t() { return "A"; } };
struct B : A { std::string t() override { return "B"; } };
int main() {
A* p = new B();
std::cout << (dynamic_cast<B*>(p) != nullptr) << " "
<< (dynamic_cast<A*>(p) != nullptr) << "\n";
delete p;
}
Virtual
4 attempts left

Answer & explanation