Static vs Non-Static Variables

What are the primary differences between static and non-static variables?

In C++, the primary difference between static and non-static variables lies in their lifetime and scope.

Non-static variables are instance variables, meaning each instance of a class has its own copy of these variables. For example, if you have a Vampire class with a Health variable, each Vampire object will have its own Health:

#include <string>

class Vampire {
public:
  int Health{100};
};

int main() {
  Vampire A;
  A.Health = 50;

  Vampire B;
  B.Health = 200;
}

In this example, Vampire A has a Health of 50, while Vampire B has a Health of 200. Each instance operates independently with its own copy of Health.

Static variables, on the other hand, are shared among all instances of a class. They belong to the class itself, not to any particular instance. When a variable is declared as static, all instances of the class share the same memory location for that variable:

#include <iostream>

class Vampire {
 public:
  static inline std::string Faction{"Undead"};
};

int main() {
  Vampire A;
  Vampire B;
  if (&A.Faction == &B.Faction) {
    std::cout << "They're the same";
  }
}
They're the same

Here, Faction is static, meaning it is shared by all Vampire objects. Changing Faction for one Vampire changes it for all:

#include <iostream>

class Vampire {
 public:
  static inline std::string Faction{"Undead"};
};

int main() {
  Vampire A;
  Vampire B;
  A.Faction = "Demonic";
  std::cout << B.Faction;  
}
Demonic

In summary:

  • Non-static variables: unique to each instance, different instances can have different values.
  • Static variables: shared across all instances, one value for the entire class.

Understanding the difference helps you decide whether a variable should be unique to each instance or shared among all instances.

Static Class Variables and Functions

A guide to sharing values between objects using static class variables and functions

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Combining Static and Non-Static Variables
Can we have both static and non-static variables in the same class? How do they interact?
Memory Allocation for Static Variables
How does marking a variable as static impact memory allocation?
The Inline Keyword for Static Variables
Why is the inline keyword necessary for static variables in header files?
Initializing Static Variables in the Constructor
Can static class variables be initialized in the constructor of a class? Why or why not?
Static vs Global Variables
How do static variables differ from global variables in C++?
Static Functions Use Cases
What are some common use cases for static functions?
Static Member Functions
Can static member functions access non-static members? Why or why not?
Advantages of Static Members
What are the advantages and disadvantages of using static members?
Initialization Order of Static Variables
How does C++ handle the initialization order of static variables in different translation units?
Static Members in C++ vs Other Languages
How does the concept of static members in C++ compare to similar concepts in other programming languages like Java or C#?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant