ProgrammingC++ Developer

What is static_cast in C++ and how is it different from other types of casting?

Pass interviews with Hintsage AI assistant

Answer.

In C++, type casting allows you to explicitly specify to the compiler how to convert an object from one type to another. In earlier versions of C++ (C++98), C-style casts ((int)x) were used for this purpose. However, this approach is implicit and often leads to errors, as the compiler cannot check the correctness or safety of the conversion. To improve safety, specialized type casting operators were introduced, among which static_cast plays an important role.

Background:

Before the introduction of the static_cast keyword, developers frequently encountered errors due to implicit conversions. With the introduction of static_cast in C++98, it became possible to explicitly distinguish different intentions and ensure that casting occurs only where it is logical at compile time.

Problem:

Often, it is necessary to convert compatible types (for example, numeric values or pointers to related inherited classes), but to do this transparently and safely. Standard C-style casting implicitly mixes compiler-checkable and potentially dangerous conversions.

Solution:

static_cast is designed for explicit, but statically checked casting of compatible types. It is safer than ordinary C-style casting and does not allow casting completely incompatible types at compile time.

Example code:

class Base { }; class Derived : public Base { }; Base* b = new Derived(); Derived* d1 = static_cast<Derived*>(b); // correct, if b actually points to Derived int x = 10; double y = static_cast<double>(x); // works

Key features:

  • Allows only those conversions that are permissible at compile time
  • Does not perform runtime checks (unlike dynamic_cast)
  • Used for "ordinary" conversions between base and derived classes, numeric types, pointers, void*

Trick questions.

Can you use static_cast to convert between completely unrelated types, like int and double**?**

No, the compiler will not allow such casting without an explicit intermediate conversion through void*, since the types are not directly related. For example:

int* p1; double* p2 = static_cast<double*>(p1); // compilation error

Can you use static_cast for a safe "downcasting" from a base class to a derived one? What happens if the pointer does not point to a derived type object?

static_cast can perform such casting, but it does not check the real type at runtime. If the base pointer does not point to an object of the desired derived class, the result will be undefined behavior:

Base* base = new Base; Derived* wrong = static_cast<Derived*>(base); // UB! Not the right type

How does static_cast behave with private inheritance?

static_cast will not allow casting a pointer or reference from a base class to a derived one through private or protected inheritance outside the inheriting class or its friends — a compile-time error will occur.

Common mistakes and anti-patterns

  • Using static_cast for casting between unrelated types
  • Using static_cast for "downcasting" without runtime type checking
  • Trying to bypass access restrictions (public/protected/private inheritance)

Real-life example

Negative case

A programmer mindlessly uses static_cast to convert any pointer from a base class to derived, without checking the actual underlying type (for optimization speed, for example).

Pros:

  • Code compiles and works in tests
  • No runtime overhead (like with dynamic_cast)

Cons:

  • When new inheritors or different logic appear, it immediately results in undefined behavior
  • Hard to track the cause of program crashes

Positive case

Using static_cast only after checking the object's type through additional metadata or design (or for safe numeric type conversions).

Pros:

  • Safety and transparency
  • Optimal execution speed

Cons:

  • May require additional design or infrastructure for safe use
  • Not all conversions are possible without runtime checks