Static Class Variables and Functions

Static vs Global Variables

How do static variables differ from global variables in C++?

Abstract art representing computer programming

Static variables and global variables in C++ both have static storage duration, meaning they exist for the lifetime of the program. However, there are key differences between them in terms of scope, usage, and encapsulation.

Global Variables

  • Scope: Global variables are accessible from any part of the program. They are defined outside of any class or function.
  • Visibility: By default, global variables have external linkage, meaning they can be accessed from any translation unit in the program.
  • Encapsulation: Global variables are not encapsulated within a class, which can lead to potential naming conflicts and unintended modifications.

Example of a global variable:

#include <iostream>

int globalVar{100}; 

void printGlobal() {
  std::cout << "Global Variable: "
    << globalVar << "\n";
}

int main() {
  printGlobal();
  globalVar = 200;
  printGlobal();
}
Global Variable: 100
Global Variable: 200

Static Variables

  • Scope: Static variables within a class are only accessible through the class. They do not have global scope.
  • Visibility: Static variables have internal linkage when defined within a class, meaning they are only accessible within the scope of the class.
  • Encapsulation: Static variables are encapsulated within the class, promoting better organization and reducing the risk of naming conflicts.

Example of a static variable:

#include <iostream>
#include <string>

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

void printFaction() {
  std::cout << "Faction: "
    << Vampire::Faction << "\n";
}

int main() {
  printFaction();
  Vampire::Faction = "Demonic";
  printFaction();
}
Faction: Undead
Faction: Demonic

Key Differences

  • Scope: Global variables are accessible everywhere, while static variables are scoped to the class.
  • Encapsulation: Static variables are encapsulated within the class, improving organization and maintainability.
  • Visibility: Global variables have external linkage by default, whereas static variables within a class have internal linkage.

When to Use Each

  • Global Variables: Use global variables sparingly, typically for constants or configurations that need to be accessed globally.
  • Static Variables: Use static variables within classes to share data among all instances of the class while maintaining encapsulation.

In summary, static variables provide better encapsulation and scope control compared to global variables, making them preferable for shared data within classes.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved