Static Classes

Is C++ Statically or Dynamically Typed?

C++ is statically typed, so the compiler checks types before the program runs. It still has some runtime type features, such as virtual functions.

On this page
  1. Short answer
  2. What static typing means
  3. Errors caught before the program runs
  4. Runtime type features in C++
  5. How this compares with Python
  6. What this means for learning C++

Short answer

C++ is statically typed. The type of every variable and expression is fixed when the program is compiled, and the compiler checks type rules before the program runs. C++ also has a few runtime type features, so the rule is not purely compile time.

What static typing means

In a statically typed language, each variable has a type that the compiler knows. Assigning a value of an incompatible type is an error. Here is an example:

#include <iostream>

int main() {
    int count = 5;
    // count = "five";           // compile error: no conversion from a string to int
    count = 7;                   // fine: count is still an int
    auto total = count * 2.5;    // the compiler deduces total as double
    std::cout << count << " " << total << "\n";  // prints 7 17.5
}

The auto keyword does not make C++ dynamic. It asks the compiler to work out the type when it compiles the code, and that type is fixed after that.

Errors caught before the program runs

Because the compiler knows each type in advance, it can reject many mistakes before the program runs. Passing a string where a function expects an int, or calling a function that does not exist, fails to compile. A bug in the program’s logic still shows up only when the program runs, so compile-time checks do not catch everything.

Runtime type features in C++

Some C++ features look up type information while the program runs. They do not change the fact that variable types are fixed at compile time.

  • Virtual functions. Calling a virtual function through a base-class pointer or reference runs the version for the object’s actual type. This runtime choice is the core of polymorphism in c.
  • dynamic_cast. Checks at runtime whether a pointer or reference to a base class refers to an object of a derived type. It works with classes that have virtual functions.
  • typeid. For a class with virtual functions, reports the type of the object at runtime.
  • std::any. Added in C++17, it holds a value of any copyable type. When you read the value with std::any_cast, the stored type is checked at runtime.

How this compares with Python

Python is dynamically typed. A variable name can refer to a value of one type and later to a value of another type:

x = 5
x = "five"

Python accepts both lines. In C++, a variable declared as int stays an int for its whole life, so the second assignment would be a compile error.

What this means for learning C++

Declare variable types clearly, or use auto when the type is obvious from the right-hand side. When the compiler reports a type error, read the message closely, since it usually points to the exact mismatch.

The complete guide to static classes

Static Classes in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

The using Keyword in C++

The using keyword in C++ brings names into scope, creates type aliases and exposes base class members. Here is each form with short examples.

using 5 min read

Struct vs Class in C++

In C++, struct and class differ mainly in default access: struct members are public, class members are private. See when to use each, with examples.