Why does C++ reject dynamic_cast<Player*>(e) when Entity has no virtual functions, and what does typeid(*e) report then?
answer
- no virtual function, no dynamic type
- the run-time check needs RTTI
- upcasts are exempt
- typeid falls back to static type
basics
~20 sA downcast with dynamic_cast needs a polymorphic source type, one with at least one virtual function, because only then can the object's dynamic type be found at run time. For a non-polymorphic Entity, typeid(*e) reports the static type, Entity.
solid answer
~50 sThe standard calls a class with a virtual member function **polymorphic**, and `dynamic_cast` needs one for every conversion that is not a plain upcast: the operand must be a pointer to, or glvalue of, a polymorphic type. The reason is mechanical: the run-time check must discover the object's **dynamic type**, and implementations attach that information to polymorphic objects only, typically through the virtual table pointer. Clang rejects the cast with `'Entity' is not polymorphic`. Converting to the same type or up to an unambiguous, accessible base still compiles, since no check is needed. `typeid` follows the same split: on a glvalue of polymorphic type it reports the dynamic type, but on a non-polymorphic `Entity` it reports the **static** type, so `typeid(*e).name()` names `Entity` even when the object is a `Player`. Adding `virtual ~Entity() = default;` fixes both.
code
cpp · 12 lines#include <iostream>
#include <typeinfo>
struct Entity { int id = 0; }; // no virtual functions
struct Player : Entity { int score = 0; };
int main() {
Player hero;
Entity* e = &hero;
std::cout << typeid(*e).name() << '\n'; // static type: Entity
Entity* up = dynamic_cast<Entity*>(&hero); // upcast: fine without virtuals
(void)up;
// Player* p = dynamic_cast<Player*>(e); // error: Entity is not polymorphic
}go deeper
Remember the rule: no virtual function in the base, no dynamic_cast downcast, and the cure is usually a virtual destructor.
Explain static versus dynamic type, which dynamic_cast cases need a polymorphic source, and why typeid falls back to the static type.
Recognise the symptom in logs that always name the base type, and connect the fix to safe deletion through base pointers.
Decide which hierarchies should be polymorphic at all, weighing per-object overhead against the need for checked downcasts and safe deletion.
## Static type and dynamic type Every expression has a **static type**, the type the compiler sees in the source. An object reached through `Entity* e` has static type `Entity`. The object itself may be a `Player`; that is its **dynamic type**, the type of the most derived object it belongs to. Run-time type identification, **RTTI**, is the machinery that recovers the dynamic type while the program runs. Two operators use it: `dynamic_cast` and `typeid`. ## The rule in the standard The standard defines a **polymorphic class** as a class with a virtual member function, declared or inherited. `dynamic_cast` then splits its cases: | Conversion | Needs a polymorphic source? | |---|---| | to the same type | no | | up to an unambiguous, accessible base (`Player*` to `Entity*`) | no: an ordinary conversion | | down (`Entity*` to `Player*`) | **yes** | | across, between sibling bases | **yes** | | to `void*`, the most derived object | **yes** | For everything except the first two rows, the operand shall be a pointer to, or a glvalue of, a polymorphic type. Breaking that rule is a compile-time error, not a run-time failure. Clang reports: `error: 'Entity' is not polymorphic`. ## Why the rule exists To answer "is this `Entity` really a `Player`?", the program must find the object's dynamic type from the object alone. Common implementations store a pointer to a virtual table in every polymorphic object, and the table leads to the class's `std::type_info` and its base-class description. A class with no virtual functions has no such pointer, so there is nothing to read; the language therefore refuses the check rather than inventing per-object overhead for every class. The virtual-table layout itself is the implementation's choice, but the requirement is the standard's. ## What `typeid` does without virtual functions `typeid` does not reject non-polymorphic types. Instead it changes meaning: - Applied to a glvalue of **polymorphic** class type, it evaluates the operand and returns the `std::type_info` of the **dynamic** type. - Applied to anything else, it returns the `std::type_info` of the **static** type, and the operand is **not evaluated**. So with a non-polymorphic `Entity`, `typeid(*e)` describes `Entity` even when `e` points to a `Player`. The code example prints `6Entity`, Clang's mangled name for `Entity`, for an object that is really a `Player`. Because the operand is unevaluated, `*e` is not even dereferenced. The code compiles and silently gives the less useful answer, which is why logging code that "always prints Entity" is a classic symptom. ## The fix and its side effects Declaring a virtual destructor makes `Entity` polymorphic: ```cpp struct Entity { virtual ~Entity() = default; }; ``` 1. `dynamic_cast<Player*>(e)` compiles and checks the dynamic type. 2. `typeid(*e)` reports `Player` for a `Player`. 3. Deleting a `Player` through an `Entity*` becomes well defined, which a hierarchy used through base pointers needs anyway. The cost is the implementation's per-object virtual table pointer, usually one pointer, plus the RTTI data emitted for the class. For a type that is only ever handled by value and never downcast, that cost buys nothing; for an entity hierarchy, it is already paid. ## Polymorphism is inherited A class is polymorphic if it has a virtual member function, and inherited virtual functions count. Once `Entity` declares a virtual destructor, `Player`, `Monster` and every class below them are polymorphic too, so a `dynamic_cast<Boss*>` from a `Player*` also compiles. An abstract base with pure virtual functions is polymorphic for the same reason. The reverse also holds: if only a derived class adds a virtual function, the base is still not polymorphic, and casts that start from a base pointer still fail to compile. Making a class polymorphic only so that code can downcast it is worth questioning. If nothing ever deletes through the base and no behaviour varies by type, the downcast itself may be the design problem, and the alternatives covered in the question on RTTI cost may fit better. ## What interviewers listen for - That `dynamic_cast` needs a polymorphic type only for checked conversions; an upcast compiles regardless. - That a non-polymorphic `typeid` quietly reports the static type rather than failing. - That the fix is a virtual destructor, and that it serves deletion as well as RTTI.
- In C++, does dynamic_cast<Entity*>(playerPtr) compile when Entity has no virtual functions?Yes. Converting a `Player*` to a pointer to an unambiguous, accessible base is an ordinary up-conversion; the standard defines that case before it requires a polymorphic type, so no run-time check is needed. It behaves exactly like the implicit conversion `Entity* e = playerPtr;`. Only downcasts, cross-casts and casts to `void*` need a polymorphic source.
- Why is the operand of typeid(*e) not evaluated when Entity is not polymorphic in C++?For anything other than a glvalue of polymorphic class type, the standard makes the `typeid` operand an unevaluated operand: the answer is the static type, which the compiler already knows, so nothing needs to run. That is why `typeid(*e)` with a non-polymorphic `Entity` neither dereferences `e` nor throws for a null `e`; it just describes `Entity`.
- dynamic_cast compiles on any class and fails at run time instead.
- typeid always reports the dynamic type of the object.
- An upcast with dynamic_cast also requires a virtual function.
- Any data member is enough to make a class polymorphic.
- The standard mandates a vtable pointer as the place RTTI lives.