TRT
Sign in
mediumAdvancedconcept

When would you use each of the four named casts, and why is const_cast dangerous on truly const objects?

Covers static, dynamic, const, and reinterpret cast use cases, the polymorphic requirement for dynamic_cast, and the undefined behavior of writing through const_cast on immutable storage.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstCasts With Intent: static, dynamic, const and reinterpretPro — members onlyConvert with the narrowest cast that states intent, check polymorphic downcasts at runtime, and remove const only from values that began life mutable.

A strong answer

A strong candidate frames When would you use each of the four named casts, and why is const_cast dangerous on truly const objects? around the exact memory layout, compiler semantics, and hardware execution model covered in Casts With Intent: static, dynamic, const and reinterpret. Covers static, dynamic, const, and reinterpret cast use cases, the polymorphic requirement for dynamic_cast, and the undefined behavior of writing through const_cast on immutable storage.

A hierarchy with a virtual display function holds base pointers that sometimes point at derived objects. The program must call a derived only operation when it exists and report failure otherwise. An unchecked conversion assumes the target type and proceeds, which calls a function on memory that lacks the derived part when the assumption is wrong. The trace above shows the checked alternative, where the first downcast succeeds and the second yields null for an explicit branch.

Arithmetic conversion adds a second need. A double holding a fractional value must become an integer by truncation, with the loss made visible in code. A legacy API takes a mutable reference while the caller holds the value through a const view of mutable storage. Raw byte inspection needs a view of an integer as bytes for logging. Each need maps to one cast, and the name of the cast documents the risk accepted.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: Choose static, dynamic, const or reinterpret cast for a given conversion.
  • Mastery of core invariant: Check dynamic downcasts for null and explain the polymorphic requirement.
  • Mastery of core invariant: Apply const cast safely to non-const origins without undefined behavior.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01Why does dynamic_cast require a polymorphic base class?
  2. 02What is the aliasing risk of reinterpret_cast?

Not started

Sign in to save your learning progress.

Sign in to save