The static modifier example in c++ is explained in detail

  • 2020-05-30 20:54:27
  • OfStack

preface

This article mainly introduces the content of static modifier in c++, which is Shared for your reference and learning. Without further discussion, let's take a look at the detailed introduction.

The following paragraph is a quote from effective c++ :

The lifetime of the so-called static object is from the time it is constructed to the end of the program (this article will not be redundant). So both stack and heap-base objects are excluded. Such objects include global objects, objects defined in the namespace scope, objects in classes, objects in functions, and objects declared as static in the file scope.

Therefore, static can exist in c++ in the following situations:

1. Static variables that exist in the global scope


// Globally accessible, only in memory 1 A copy that can be modified by many functions. 
#include <iostream>

static int i = 1; // The scope is the whole thing file

void get(){
 std::cout << "in func , the i is " << i << std::endl;
}

int main(){
  std::cout << "the i is " << i << std::endl;
  get();
  return 0;
}

2. Static variables that exist in functions


//  It can only be called in this function. 
//  When the function call is over, 1 Local variables are generally recycled and static variables are still present 
#include <iostream>

void get(){
  static int i = 1;
  std::cout << "the i is " << i << std::endl;
  i++;
}

int main(){
  get(); // i = 1
  get(); // i = 2
  std::cout << "the i is " << i << std::endl; //  This is wrong 
  return 0;
}

3. Static variables that exist in the member variables of a class


// In fact, the principle is similar to that of static variables in a function. When an object is instantiated by a class and destroyed, 
//  But class variables ( Static member variable ) It's still in memory 
#include <iostream>

class Widget{
public:
  Widget(int i){
    a = i; 
  }
  void get();
private:
  static int a; //  Declare static variables 
};

int Widget::a = 1; //  Because it's a class variable that doesn't belong exclusively to 1 Is Shared by all objects 
     //  So you need to define it outside the class 
void Widget::get(){
  std::cout << "the a is " << a++ << std::endl;
}

int main(){

  Widget w(1);
  w.get(); // a = 1
  w.get(); // a = 2
  return 0;
}

4. Static variables that exist in member functions of a class


#include <iostream>

class widget{
 public:
 widget(){}
 void get();
};

void widget::get(){
 static int i = 1;
 // The scope of a static variable in a member function is the same as that of a normal local variable 1 The sample of 
 std::cout << "in func, the i is " << i++ << std::endl;
}

int main(int argc, char const* argv[])
{
 widget w1;
 w1.get(); // in func, the i is 1
 widget w2;
 w2.get(); // in func, the i is 2
 return 0;
}

5. Static variables that exist in the command space


#include <iostream>

namespace Widget {
 static int i = 1; //  Is available in this namespace 

 void get(){
 std::cout << "the i is " << i++ << std::endl;
 }
} // namespace Widget

int main (){

 using namespace Widget;
 get(); //the i is 1
 get(); // the i is 2
 return 0;
}

6. Static functions that exist in the global scope


//  In fact, with 1 The delta function is similar, 
//  But it limits the function's link properties to inner links, 
// Can only be used in this compilation unit ( That's this file ) . 
// Can't be extern Is referenced in an external file 
static void get(){
 std::cout << "this is staic global func" << std::endl;
}

int main(){
 get();
 get();
 return 0;
}

7. Static functions that exist in a class


#include <iostream>

class Widget{
 public:
 Widget(int i){
  a = i;
 }
 static void get(); //  Declare static functions 
 
 private:
 static int a;
 int b;
};

int Widget::a = 1;
void Widget::get(){
 std::cout << b << std::endl; // This is wrong because static functions and static variables can directly 
         // Widget::get() Call, you don't need to instantiate, so you can't 
         //  A call to a member variable that can only be instantiated. 
 std::cout << a << std::endl; //ok
}

int main(){

 Widget w(1);
 w.get();
 return 0;
}

Conclusion:

Whatever the static variable, its lifetime is from the time it is constructed to the end of the program.

static variables of type is different from other common variables in memory form different, exist in the local variables of functions, for example, one function called whenever, can produce a local variable, but in the function of static variables only when the functions are called first initialized, then, and only keep 1 copy in memory

supplement

There are three types of link properties:

1. In the link

2. The external links

In the link:

Functions and variables modified by static and variables modified by const (not including extern) are internal links that can only be used in this file, even if other files define the same variable names.

External links:

Global variables or functions that are not modified by static can be used as external links. Global variables or functions that are modified by extern can also be used as external links. And 1 extern const int i = 1; This is also an external link, because extern overrides const to make it an external link.

There is also a class 1: local variable, whose lifetime is only during the execution of the function, so it has no link properties.

A constant member function cannot modify a member variable in a class, but a static member variable is a class variable, so it can be modified


#include <iostream>

class Widget{
 public:
 Widget(int i){
  b = i;
 }
 void set() const;
 private:
 static int a;
 int b;
};

int Widget::a = 1;
void Widget::set() const{
 a++; // That's right, because it's a static member variable and it's a class variable 
 b++; // Wrong, ordinary member variables cannot be changed by constant functions. 
   
}

conclusion


Related articles: