Of the "const" and "static" is understood the depth - pretreatment, const, static and sizeof (II)

table of Contents

 

use const

const modified type integer constant

Where const modifier pointer type variable

const specific application scenario

static understanding

The use of static

The difference between the modified static global / local variables / functions and normal global / local variables / functions

The difference between static and global variables are modified ordinary global variables

The difference between the modified static local variables and local variables of the ordinary

The difference between the modified static function and a normal function

Case of static objects created in class

Small case


use const

const modified type integer constant

#include <stdio.h>

int main()
{
	const int x = 1;//将x定义为整型常量,设置为不可更改的属性。

	x = 2; //会报错,因为x不可被更改

	system("pause");
	return 0;
}

Where const modifier pointer type variable

#include <stdio.h>

int main()
{
	int b = 10;
	int c = 20;

	const int* a1 = &b; //将变量的类型设为固定值
	int* const a2 = &b; //将变量本身设为固定值
	const int* const a3 = &b;//将变量的类型及本身都设定为固定值

	a1 = &c; //正常运行
	//*a1 = 1;//报错

	//a2 = &c;
	*a2 = 1;

	//a3 = &c;
	//*a3 = 1;

	system("pause");
	return 0;
}

In "a1 = & c;" for example, can not be modified because of the type a1, a1 itself but the stored value can be modified, so that the variable itself a1 stored value into the address c, it can be successful.

With "// * a1 = 1;" Similarly, a1 type can not be modified, go to * a1 = 1, means that the content stored in the address a1 variable points to a variable changes to 1, so why amend the pointer points the content will affect the pointer type it? This is because the content pointer and it points to the type of memory storage space consistent, plus the type of pointer const, where there is a double meaning: not only to ensure that the type of data pointed to by the contents can not be changed, is to ensure points to the content itself can not be modified.

Words summarize, when used to modify const int * a, int * const on the left, then int * Content created by the pointer is stored in the memory space, can not be modified; const int * in the right hand, that int * pointer variable created can not be modified.

const specific application scenario

  1. const is used to define constants, the compiler will be data type static type checking.
  2. const modified form of the function parameters (parameters), to ensure that the input parameters will not be modified in function.
  3. const modified return value of the function, if the return value is modified by const, the return value can not be modified directly, but also should be a pick up with a definition of constant const return value of this job.
  4. const modified class member function (function definition body), does not modify any function of a member of function, should be used to modify const.

static understanding

The use of static

  1. In the body of the function, it is declared as a static variable remains unchanged in its value is a function of the body.
  2. In scope (within the module), but in function in vitro, maintain this is declared as static variables can be accessed by all functions in scope, but can not be accessed outside of the scope of the function. Equivalent exists in the global scope of the variables.
  3. Similarly, with a statically defined function , but also can be used only in (modules within) the current scope, it can be called by other functions.

The difference between the modified static global / local variables / functions and normal global / local variables / functions

The difference between static and global variables are modified ordinary global variables

In large projects, related to the cpp file may have many, if defined in which a global variable, its scope is the source, and the source of large-scale projects may be composed of a good number of the source, which it is caused by a non-static global variables are common variables that is valid in every source program; been modified static global variable, that variable is defined only in the moment of the effective scope, it can only be under the scope the function is used, the program can not be called outside of the scope, so you can avoid causing cited the risk of errors in other source files.

Throughout static variable is initialized only once.

The difference between the modified static local variables and local variables of the ordinary

If a local variable has not been modified by static, and that its life cycle, it is only in their function in the middle, but was later modified static, its life cycle, it expanded to the current scope, that is, until the scope of All programs run finished, it will be destructed off. That is, static can change the life cycle of local variables.

Throughout static variable is initialized only once.

The difference between the modified static function and a normal function

Modified static function may only be used in its scope this created, if the non-scope would like to call it, it must declare it in a header file, so when I wanted to call a function using the function of , this header file to be included. ,

Moreover, static a unique function in memory, while the ordinary function creates a copy out each time it is called in.

Case of static objects created in class

If you are creating a static variable in a class, the object is created when the scope is called the first time out, and from beginning to end only created once and can not be modified, be destroyed after running the main function exits; static variables belongs to the entire class without any object belonging to the class created, static object variable to store only one, want to call it that this is a function of both public data.

Small case

#include <stdio.h>
#include <iostream>

class widget
{ 
public:
	widget() {
		count++;
	}

	~widget() {
		--count;
	}
	static int num(){
		return count;
	}
private:
	static int count;
};

int widget::count = 0;

int main()
{
	widget x, y;
	printf("创建对象的个数:%d\n", widget::num());
	
	system("pause");
	return 0;
}

The use of static this feature, you can do some very interesting things, as the code, you can use static variables defined, is not affected by the object being created, you can complete count object of a class that is created. operation result:

Published 271 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_17846375/article/details/104925232