Mastering C++: Part 1: Static

Learning C++

Using C++

Mastering C++


When thinking in the object-oriented paradigm, there is often a need to share values across different object instances. When creating new ones, there will always been a single value that all can access and use. The keyword static helps with this.

Any variable or function defined as static will be defined once for all objects.

However, while the use of static will define it (allocate memory for it, in most cases), it must be given a value outside of it.

Instances and Shared Values

Normally, each new object has its own memory and values. With using the keyword static, values can be shared across all of them.

Play with the example on!