Combining Static and Non-Static Variables

Can we have both static and non-static variables in the same class? How do they interact?

Yes, you can have both static and non-static variables in the same class. They serve different purposes and interact in specific ways.

Static variables are shared across all instances of the class, while non-static variables are unique to each instance.

This means that each instance of the class will have its own copy of non-static variables, but there will be only one copy of each static variable, regardless of the number of instances.

Consider this example:

#include <iostream>
#include <string>

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

int main() {
  Vampire A;
  Vampire B;

  A.Health = 50;
  B.Health = 200;

  A.Faction = "Demonic";

  std::cout << "A's Health: " << A.Health << "\n";       
  std::cout << "B's Health: " << B.Health << "\n";       
  std::cout << "Faction: " << Vampire::Faction;  
}
A's Health: 50
B's Health: 200
Faction: Demonic

In this example:

  • Health is a non-static variable, so each Vampire instance (A and B) has its own Health.
  • Faction is a static variable, so it is shared across all instances. Changing Faction through A also changes it for B.

When you have both static and non-static variables in a class:

  • Non-static variables are accessed through instances of the class.
  • Static variables can be accessed through instances or directly through the class itself using the scope resolution operator ::.

For example, you can access Faction directly using Vampire::Faction:

#include <iostream>
#include <string>

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

int main() {
  Vampire A;
  Vampire B;

  Vampire::Faction = "Demonic";
  std::cout << "Faction: "
    << Vampire::Faction << "\n";  

  A.Health = 50;
  B.Health = 200;
  std::cout << "A's Health: " << A.Health << "\n";  
  std::cout << "B's Health: " << B.Health << "\n";  
}
Faction: Demonic
A's Health: 50
B's Health: 200

Combining static and non-static variables allows you to maintain shared state and individual state within the same class.

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.

Static vs Non-Static Variables
What are the primary differences between static and non-static variables?
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