Read c++ static keywords

  • 2020-10-07 18:47:10
  • OfStack

1. Static variables

As with C language 1, you can use static to describe automatic variables. According to the location of the definition, static global variables and static local variables.

A global variable is a variable that is declared outside of all curly braces and whose scope is globally visible, that is, valid throughout the project file. A global variable decorated with static is a static global variable with a limited scope that is valid only in the source file where it is defined and cannot be used in other source files in the project. Variables defined within a block are local variables, and the scope of the local variable begins where it is defined and ends at the end of the block. A local variable decorated with static is a static local variable, that is, a static variable defined in a block. Static local variables have local scope but global lifetime. Static local variables have local scope but global lifetime. That is, a static local variable exists for the entire duration of the program, occupies space 1 that is not released until the end of the program, but is valid only in the block that defines it, and cannot be accessed outside the block. Static variables are stored in the global data area, and static local variables are initialized only once. If the program does not explicitly give an initial value, it is equivalent to initializing to 0; If the initial value is given explicitly, the initialization is completed on the first execution of the block in which the static variable resides.

#include<iostream>
using namespace std;

// Global static variable 
static int glos_s=10;
// The global variable 
int glos_a=10;

void f(){
  int a=1; // A local variable a
  static int fs=1; // Static local variable fs , complete initialization. 
  a+=2;
  fs+=2;
  glos_s+=10;
  glos_a+=10;
  cout<<" in f In the : A local variable a="<<a<<"  Static local variable fs="<<fs<<"  The global variable glos_a="<<glos_a<< "  Global static variable glos_s=" << glos_s <<endl;
}
int main(){
   f(); // in f In the : A local variable a=3  Static local variable fs=3  The global variable glos_a=20  Global static variable glos_s=20
   f(); // in f In the : A local variable a=3  Static local variable fs=5  The global variable glos_a=30  Global static variable glos_s=30
  return 0; 
}

2. static in class

1. Define static members of a class

There are two types of static members of a class: static member variables and static member functions. When you define members of a class in the body of a class,
When the static keyword is added before, the member becomes a static member.

A static member of a class is Shared by all objects of the class. No matter how many objects exist, only 1 static member is held in common memory. For static member variables, each object sees one value. When you define a class static member variable, you declare the static member variable in the class definition, and then you must define the initial value of the static member variable outside the class. This initial value cannot be assigned in the class body. The format for assigning initial values to static member variables is as follows: Type class name :: Static member variables = initial values; Note that when assigning initial values to static member variables outside the class, the static keyword should not be prefixed to avoid confusion with static variables like 1. The static keyword is also not allowed when defining member functions outside of a class.

2. Use of class static members

When a static member is accessed, the member can be prefixed by either the class name or the object name or object pointer. This is different from accessing class members only by prefixing the object name or object pointer. The general format for accessing static members of a class is as follows:

Class name :: Static member name Object name. Static member name Object pointer - > Static member name

Static member functions of a class do not have this Pointers, and non-static members cannot be accessed within static member functions, that is, static member functions of a class can normally only handle static member variables of the class. Non-static member functions cannot be called within static member functions.


#include<iostream>

using namespace std;

class Student{
  public:
    int id;
    string name;
    static int money;
    static void printlnMoney(Student &stu);  
};
// Static members must be initialized outside of the class , And cannot be assigned to a class body 
int Student::money=10000;
// Implementation of static member functions 
void Student::printlnMoney(Student &stu){
  cout << "name:" << stu.name <<" money:" << stu.money << endl;
}

int main(){
  Student stu;
  stu.id=1;
  stu.name=" millet ";
  // Use an object to invoke a static member 
  stu.printlnMoney(stu);//name: millet  money:10000
  Student *stu2=new Student();
  stu2->id=1;
  stu2->name=" Meituan ";
  // A static member is invoked using a pointer 
  stu2->printlnMoney(*stu2);//name: Meituan  money:10000
  // The static member is invoked using the class name 
  Student::money=50000;
  Student::printlnMoney(*stu2);//name: Meituan  money:50000
  Student::printlnMoney(stu);//name: millet  money:50000
  return 0; 
}

3. Normal and static members

For ordinary member variables, each object has its own copy, while static member variables have only one copy and are Shared by all objects of the same class. Ordinary member functions 1 must be applied to an object, while static member functions are not applied to an object.

Ordinary members are accessed through the object name. ", indicating which object the member variable to be accessed belongs to, or which object the member function to be called functions on;

When a static member is accessed, it can be accessed by "class name :: member name" without specifying which object the member being accessed belongs to or works on.
Thus, you can access the static members of a class even before any objects have been generated. The way non-static members are accessed also works for static members, that is, through the object name. "Member name" has the same effect as "class name :: member name"

In general, all but one of the member functions of a class are allowed to call each other. Specifically, static and static functions, non-static and non-static functions can be called between each other, non-static member functions can be called static member functions, but not static member functions can not be called static member functions

The above is 1 article read c++ static keyword details, more about c++ static keyword information please pay attention to other related articles on this site!


Related articles: