Static Classes

Static Classes in C++

C++ has no static class keyword. Here is how static members, utility classes and namespaces cover the same job, with short code examples for each.

On this page
  1. Does C++ have a static class?
  2. What static means inside a class
  3. A class with only static members
  4. A namespace as an alternative
  5. Static class in C# and Java
  6. Static data and initialization order
  7. Design trade-offs
  8. Which to choose
  9. Common mistakes

Does C++ have a static class?

No. C++ has no static class keyword. A top-level class in C++ cannot be declared static.

What people usually mean by a static class is a group of functions that belong together, with no objects created from them. C++ does not have a special kind of class for this, but you can get the same result in two common ways: a class where every member is static, or a namespace. This article covers both, along with the rules for the static keyword that make them work.

What static means inside a class

Inside a class, static makes a member belong to the class itself rather than to each object. There are two kinds:

  • A static data member is one variable shared by all objects of that class. It exists even when there are no objects at all.
  • A static member function is called on the class. It has no this pointer, so it cannot read or change non-static members unless you pass it an object.

Do not confuse a static member function with the file-level functions described in static functions in c. The two share a keyword but are different features.

Here is a small example:

#include <iostream>

class Counter {
public:
    static int count;            // declared inside the class

    static void increment() {    // static member function
        ++count;
    }
};

int Counter::count = 0;          // defined once, outside the class

int main() {
    Counter::increment();
    Counter::increment();
    std::cout << Counter::count << "\n";  // prints 2
}

The static data member is declared inside the class and defined once outside it. Before C++17, that outside definition was required, and it had to appear exactly once in the program. From C++17, you can write static inline int count = 0; inside the class instead.

You call static members with the class name and ::, such as Counter::increment(). Calling them through an object also compiles, but the class name makes it clear that the member is not tied to any one object.

A class with only static members

When every member of a class is static, the class becomes a container for related functions. To stop anyone from creating objects of it, delete the default constructor:

#include <iostream>

class MathUtil {
public:
    MathUtil() = delete;  // no objects can be created

    static int add(int a, int b) {
        return a + b;
    }
};

int main() {
    std::cout << MathUtil::add(2, 3) << "\n";  // prints 5
    // MathUtil m;  // compile error: the default constructor is deleted
}

The MathUtil() = delete; line blocks object creation, so the commented-out line in main would not compile. This is the nearest C++ match for a static class. It is a convention made from ordinary language features, so nothing stops someone from editing the class later and adding a constructor.

A namespace as an alternative

A namespace groups functions and variables under a name. It has no objects, so there is nothing to block, and it needs no constructor line:

#include <iostream>

namespace math_util {
    int add(int a, int b) {
        return a + b;
    }
}

int main() {
    std::cout << math_util::add(2, 3) << "\n";  // prints 5
}

A class can mark some static members private. A namespace has no equivalent, so every name in it is visible to any code that includes its header. If you define functions in a namespace inside a header that several source files include, mark them inline. Otherwise the linker reports duplicate definitions.

Static class in C# and Java

People who ask what is a static class are often coming from C# or Java, so here is how those languages handle the idea:

  • C# has a static class keyword. A static class can contain only static members, and it cannot be instantiated.
  • Java does not allow a top-level class to be static. A class nested inside another class can be marked static, which means it does not hold a reference to an enclosing object.

C++ nested classes never hold a reference to an enclosing object either, so they behave like Java’s static nested classes without needing a keyword.

Static data and initialization order

The language does not define the order in which static objects in different source files are initialized. If one object’s setup depends on another object in a different file, the program can fail in ways that are hard to trace.

To avoid that, keep static data simple, or use a static local variable inside a function. A static local object with a constructor is created the first time execution reaches its definition, so it does not depend on the order in which source files are set up.

Design trade-offs

Static members are shared state that lasts for the whole program. Code that depends on them is harder to test in isolation, because a test cannot easily swap them out, and a change to a static value can affect code far away. Passing in the objects a function needs, rather than reaching for shared state, is the idea behind dependency injection in c.

Static member functions also cannot be virtual, so they do not take part in the runtime dispatch behind polymorphism in c.

Which to choose

  • Use a namespace when you only need plain helper functions or constants.
  • Use a class with static members when you want a named type for the group, or when some shared data should be private. Delete the default constructor if you want to stop objects from being created.

Common mistakes

  • Missing definition. A static data member that is declared but never defined causes a linker error, often reported as an undefined reference.
  • Definition in a header. Before C++17, defining a static data member in a header that several source files include causes duplicate-definition errors. Use inline (C++17 and later) or move the definition into one source file.
  • Calling a non-static member from a static function. This does not compile, because there is no object to use.
  • Expecting per-object data. Every object of the class shares the same static data member, so a change made through one object is seen through all of them.

Keep exploring

Guides from other topics

All topics →