Static Cast

This lesson highlights the key features of the static_cast operator.

We'll cover the following

Features #

  • static_cast is the simplest casting operator and is used for simple conversions.

  • It can only perform all the conversions that are well-defined by the compiler. For example, a string to integer cast won’t work.

  • It allows bidirectional conversion between related data types such as:

    • pointer types in class hierarchies
    • integrals and floating-point numbers
    • integrals and enumerations
  • static_cast cannot be used with polymorphic types.

  • Unlike dynamic_cast, a static_cast is performed during compile time.

Example #

Get hands-on with 1200+ tech skills courses.