In C ++ static

This one just by the opportunity to introduce within the C ++ static member variables and static member functions, completely explain the static C ++

If infringement, please contact deleted, if wrong, welcome to correct me, thank you

C / C ++ common use

Static local variables

  • Static local variable that is added in front of the local variables static modification
  • Only once initialized, extending the life cycle of local variables, was released when the program ends
void func() {
	static int n;   // 静态局部变量
	cout << n++ << endl;
	cout << &n << endl;
}

// 1. 第一次调用时全局变量默认初始化为0,后面调用不再进行初始化
// 2. 在程序中多次调用该函数输出的地址是一样的,是同一变量(证明不是很严谨,局部变量也会出现这种情况)

to sum up

  1. Static local variables allocated memory space in the global / static area
  2. When a static local variable in the program execution to the statement at the object is first initialized, the function call after not initialize
  3. Usually static local variables declared in the initialization, if there is no explicit initialization, OS is automatically initialized to 0
  4. It is always resides in the global / static area until the end of the program is running, but its scope is the local scope, that is, it can not be used outside the body of the function

Static global variables

  • Static global variables can only be accessed in the current file, the other file is not accessible, nor even extern
// ====== 测试一 ======
// file: a.cpp
#include <iostream>
using namespace std;

int n;   // 全局变量
void func();

int main() {
	cout << n++ << endl;
	func();
	system("pause");
	return 0;
}

// file: b.cpp
#include <iostream>
using namespace std;

extern int n;
void func() {
    cout << n << endl;
}
// 可正常运行,运行结果:0 1

// ====== 测试二 ======
// file: a.cpp
#include <iostream>
using namespace std;

static int n;   // 静态全局变量
void func();

int main() {
	cout << n << endl;
	func();
	system("pause");
	return 0;
}

// file: b.cpp
#include <iostream>
using namespace std;

extern int n;
void func() {
    cout << n << endl;
}

// 报错

to sum up

  1. Static global variables can not be used by other files (global variables)
  2. Other variables can be defined in the same file name, not conflict

Static function (non-member function)

  • Can only be accessed in the current file, can not be invoked (similar visible static functions and static global variables) in other documents
// ====== 测试一 ======
// file: a.cpp
#include <iostream>
using namespace std;

int n;   // 全局变量
void func();

int main() {
	cout << n++ << endl;
	func();
	system("pause");
	return 0;
}

// file: b.cpp
#include <iostream>
using namespace std;

extern int n;
void func() {   // 普通全局函数
    cout << n << endl;
}
// 可正常运行,运行结果:0 1

// ====== 测试二 ======
// file: a.cpp
#include <iostream>
using namespace std;

int n;   // 全局变量
extern void func();

int main() {
	cout << n << endl;
	func();
	system("pause");
	return 0;
}

// file: b.cpp
#include <iostream>
using namespace std;

extern int n;
static void func() { // 静态全局函数
    cout << n << endl;
}

// 报错

to sum up

  1. Static functions can not be used by other files
  2. Other functions can be defined in the same file name, not conflict

C ++ specific purposes

Static member variables

  • All entity object class share the variable
class Test {
public:
	double d;
	static int n;
};

int Test::n;  // 静态成员变量需要在类外声明(分配空间,可以显式初始化,默认初始化为0)

cout << sizeof(Test) << endl;
cout << Test::n << endl;

// 运行结果:8 0

to sum up

  1. Static member variables stored in the global / static area, only static member variables allocate space only outside the class declaration, be explicitly initialized, do not explicitly initialize the default OS is initialized to 0
  2. The results of static member variables belonging to the class does not belong to the object, sizeof () does not include the static member variable size
  3. It can be obtained by calling the object, or by calling the class name scope (non-static member variables only through the object calls)

Static member function

  • Static member function can only call static member variables and static member functions
class Test {
public:
	static void show() {
		cout << n << endl;
	}

private:
	double d;
	static int n;
};

int Test::n;

cout << sizeof(Test) << endl;
Test::show();

// 运行结果:8 0

to sum up

  1. Between static members can visit each other, including a static member function to access static member variables and static member function access
  2. Any non-static member functions can access static member functions and static member variables
  3. Static member function can not access non-static member functions and non-static member variables
  4. Call a static member function can be invoked through the object, or by calling the class name scope (non-static member variables only through the object calls)

Reference article:
[1] c ++ in static usage Detailed

If not specified, the above tests are carried out under win10 vs2017 64bit compiler

Published 25 original articles · won praise 3 · Views 626

Guess you like

Origin blog.csdn.net/xiao_ma_nong_last/article/details/104068529