Summary of static keywords for C++ learning

Static can be seen everywhere in C++ programming, but its actual usage has not been summarized, so some problems often occur during use. The following is a summary of several uses of the static keyword in C++ programming as follows.

1. When a variable with the static keyword is defined inside a function, the variable is saved in the static storage area and is initialized when the program is compiled. If the initial value is not given, it will be set to 0 by default. Each time the function is called, the static variable retains the value from the last function call.

Write the following program for testing:

#include<ostream>
#include"demo.h"
#include <iostream>

using namespace Demo;
int func() {
    
    
	static int tmpval ;
	tmpval += 1;
	return tmpval;
}

int main()
{
    
    
	for (int i = 0; i < 5; i++)
	{
    
    
		int val = func();
		printf("第%d调用后,tmpval的值为%d\n", i + 1, val);
	}
	return 0;
}

The test results are as follows:
Insert image description here
After tmpval is initially initialized, the compiler will assign it a value of 0. At the end of the first program run, the result becomes 1, so the output result is 1. Each subsequent input is the last output.

2. Define a global variable with the static keyword in the code. The global variable can only work in the current file and cannot be referenced in other files.

Write the test program as follows:

//demo.h文件
#pragma once
namespace Demo {
    
    
static int iStaticVal = 10;
int add(int x, int y);
}

//demo.cpp文件
#include"demo.h"
namespace Demo {
    
    
int add(int x, int y) {
    
    
	int sum = x + y;
	iStaticVal = iStaticVal + sum;
	printf("静态变量iStaticVal=%d\n", iStaticVal);
	return sum;
}
}

//测试文件main.cpp
#include<ostream>
#include"demo.h"
#include <iostream>
using namespace Demo;
int main()
{
    
    
	int a = 10, b = 20;
	for (int i = 0; i < 5; i++)
	{
    
    
		int res = Demo::add(a, b);
		printf("外部直接打印iStaticVal的结果为:%d\n", Demo::iStaticVal);
		Demo::iStaticVal += 1;
		printf("外部调用iStaticVal后的结果为:%d\n", Demo::iStaticVal);
	}
	return 0;
}

The test results are as follows:
Insert image description here

It can be seen from the results:

  • For global variables with the static keyword, the calling scope within the file is limited to the inside of the file. When the file is out of the file, it is still initialized to the initial value 10.
  • When called in different files, the value changes only change in different files, so synchronous changes cannot be achieved in different files.

3. Add static before the function definition, then the function can only be called in the source program file and cannot be called in other source program files.

//demo.h文件
#pragma once
static int iStaticVal = 10;
namespace Demo {
    
    
int add(int x, int y);
static int mul(int a, int b);
}

//demo.cpp文件
#include"demo.h"
namespace Demo {
    
    
int add(int x, int y) {
    
    
	int sum = x + y;
	iStaticVal = iStaticVal + sum;
	printf("静态变量iStaticVal=%d\n", iStaticVal);
	int res = mul(x, y);
	printf("mul函数内部调用后结果res=%d\n", res);
	return sum;
}

static int mul(int a, int b) {
    
    
	int res = a * b;
	return res;
}
}

//测试文件main.cpp
#include<ostream>
#include"demo.h"
#include <iostream>
using namespace Demo;
int main()
{
    
    
	int a = 10, b = 20;
	for (int i = 0; i < 5; i++)
	{
    
    
		int res = Demo::add(a, b);
		printf("外部直接打印iStaticVal的结果为:%d\n", iStaticVal);
		iStaticVal += 1;
		printf("外部调用iStaticVal后的结果为:%d\n", iStaticVal);
		int cc = Demo::mul(a, b);
	}
	return 0;

}

Run the code directly and find the following error:
Insert image description here

If you comment out the code int cc = Demo::mul(a, b), then the program runs as follows:
Insert image description here
It can be seen that
the function definition with static can only be used inside the cpp file in which the function is defined, and cannot be directly used externally. Call this static function.

The above is a brief summary about static. If there are any questions, please correct me and we can make progress together.

——END——

Guess you like

Origin blog.csdn.net/caobin_cumt/article/details/131735580
Recommended