Detail c++ static member variables

  • 2020-11-20 06:10:39
  • OfStack

[

Static members are simply declared when a class is defined, and the definition and initialization of static members are done outside the class

]

C + + static The keyword modifies class member variables/methods to indicate that variables/methods do not belong to a particular object, but to a class. A close examination of static member variables shows that they are both compatible and contradictory to the C++ approach, and have their own characteristics.

Let's start with the compatible one. · C/C++ · Statement and definition: declaration gives the signature, definition gives the concrete implementation. For a type, the declaration does not always know the size of its object footprint, but by definition it certainly knows the footprint. Says that a static member is compatible with C++ because of its initialization and method definition 1. Here is an example:


// Foo.hpp
namespace tlanyan {
 //  Class declarations and definitions 
 class Foo {
 private:
  //  Declare static members 
  static int value;
 public:
  //  Method statement 
  void increaseValue();
  int getValue() const;
 };
}

// Foo.cpp
namespace tlanyan {
 //  Define and initialize static member variables 
 int Foo::value = 0;
 //  Class method definition 
 void Foo::increaseValue() {
  ++ value;
 }
 int Foo::getValue() {
  return value;
 }
}

Static member variables exhibit a more bizarre aspect than compatibility points. The following is a personal summary:

Static members cannot be initialized in a class; Non-static members can be initialized directly; static members are only declared in a class, so they cannot be initialized directly. Supplemented by const Can be initialized directly, but that is const Ability rather than static All; Static member initialization needs to be done outside the class definition; Initialization is not restricted by access modifier; private Static members of a type can be directly accessed and assigned; Static member initialization can call the function, and can directly call the class of the private function;

The fourth point is more important. On compilers that do not support C++11, to complete a static map member, you have to use the function return:


#include <map>
//  The class definition 
class Foo {
private:
 std::map<const char*, int> maps;
 ...
}
//  Static member initialization 
std::map<const char*, int> Foo::maps = Foo::initMap();
//  Or use global functions 
std::map<const char*, int> Foo::maps = initMap();

C++11 introduces series 1 initialization and lambda The expression, initialization is written more simply:


//  system 1 Initialize the 
std::map<const char*, int> Foo::maps {
 {"a", 31},
 {"b", 32}
};
// lambda Expression mode 
std::map<const char*, int> Foo::maps = [] {
 map<const char*, int> _map;
 _map.insert(map<const char*, int>::value_type("a", 31));
 _map.insert(map<const char*, int>::value_type("a", 32));
 return _map;
}();

The abnormal behavior of static members is easily associated with global variables, and there are many similarities between the two: initialization is completed before the program starts, and destruction after the program terminates; Static storage, not stacks; Gets the value from the namespace operator; Complete initialization in a non-function block via a function call or lambda expression...

Although various object-oriented programming languages have static variables, they are not used in a low proportion. But from an object-oriented perspective, static members are another form of global variables that break isolation and encapsulation, increase coupling between classes, and make testing more difficult. In real programming, global variables should be used with caution and restricted access.

So essentially static members are also global variables, just assigned to the name of a particular class.

That's c++ static member variables in detail. For more information on c++ static member variables, please follow the other articles on this site!


Related articles: