Static Class Variables and Functions

Initializing Static Variables in the Constructor

Can static class variables be initialized in the constructor of a class? Why or why not?

Abstract art representing computer programming

Static class variables cannot be initialized in the constructor of a class because they belong to the class itself, not to any particular instance.

Constructors are used to initialize instance variables, which are unique to each object created from the class.

Static variables, on the other hand, are shared among all instances of the class and are initialized outside of any instance.

Consider this example, which will not compile:

#include <string>
#include <iostream>

class Vampire {
 public:
  static std::string Faction;  

  Vampire() {
    // This will not work
    Faction = "Undead";  
  }
};


int main() {
  std::cout << Vampire::Faction;
}
error LNK2001: unresolved external symbol Vampire::Faction

Static variables need to be initialized in a context that is not tied to a specific instance of the class:

#include <string>
#include <iostream>

class Vampire {
 public:
  static std::string Faction;  
};

// Definition outside the class
std::string Vampire::Faction{"Undead"};  

int main() {
  std::cout << Vampire::Faction;
}
Undead

Typically, we would initialize static variables at the point of declaration or in a class method. Initialization at the point of declaration looks like this:

#include <string>
#include <iostream>

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

int main() {
  std::cout << Vampire::Faction;
}
Undead

In some cases, we may not be able to determine an initial value at the time we’re writing the class. In such scenarios, we can provide a static member function to set its value:

#include <iostream>
#include <string>

class Vampire {
 public:
  static std::string Faction;

  static void InitializeFaction(std::string F) {
    Faction = F;
  }
};

// Definition outside the class
std::string Vampire::Faction;

We can then call this function later, once we know what Faction should be set to:

#include <iostream>
#include <string>

class Vampire {/*...*/}; int main() { Vampire::InitializeFaction("Undead"); std::cout << Vampire::Faction; }
Undead

In summary, static class variables cannot be initialized in the constructor because they are shared across all instances and belong to the class itself. They should be initialized at the point of declaration or within a class method.

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