Use C ++ global variables and static global variables

Use of global variables

Reference links: https://www.cnblogs.com/Dageking/p/3185230.html   https://blog.csdn.net/hi_baymax/article/details/82425457

 

It is strongly recommended to create global variables and functions using the form xxx.h and xxx.cpp. .h and .cpp were to declare and define global variables. Suggested here, .h file is best to write only declare variables and functions, not to define also wrote .h file, to avoid duplicate definition of the phenomenon, we will define variables and functions written .cpp file.

Recommended that all global variables are placed in a compilation unit (ie a .h and .cpp file), so that will significantly clear and concise.

Reference form:

Header files in the form of global variables as follows: 

//************************************************************************//
/**********MyGlobalValue.h声明全局变量************/
    //* 定义各种类型的全局变量
//************************************************************************//

#pragma once

//22声明枚举来(舞蹈类型)
 enum DanceType{BIEGU_BTN,XIONGGU_BTN,YANGGE_BTN,YAOGU_BTN,NULL_BTN};
 extern DanceType enumBtnType;
 extern DanceType enumBtnType_LastFrame;

 //22声明结构体
struct fPOINT{
	float x, y;
	};
extern fPOINT fRightPoints[25];

//22声明其他常用变量
extern  char serverIP[20] ;
extern  int g_INT;
extern float g_fGMsetTM1;    //22
extern bool g_bBTN_Timeflag;   //22

//22声明全局函数
int Add(int a,int b);              //22 加法

 

 Source file in the form of global variables as follows: 

 

#include "StdAfx.h"
#include "MyGlobalValue.h"


//22定义枚举来(舞蹈类型)
 DanceType enumBtnType=NULL_BTN;
 DanceType enumBtnType_LastFrame=NULL_BTN;

 //22定义结构体
 fPOINT fRightPoints[25]={};

 //22定义其他常用变量
char serverIP[20] ={0};
int g_INT=10;
float g_fGMsetTM1=5.0f;    //22
bool g_bBTN_Timeflag=false;

//22定义全局函数
int Add(int a,int b)
{  
	int nAddResult=a+b;
   return nAddResult;
}

 Method call global variables only need to import the file header when calling to call, refer to the following:

Add the MyGlobalValue.h stdafx.h header file to file, to facilitate the compilation units with all calls

// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

#include "MyGlobalValue.h"


// TODO: 在此处引用程序需要的其他头文件
#include "stdafx.h"
//此处将“#include "MyGlobalValue.h"” 写到stdafx.h中,以方便共所有编译单元调用

int _tmain(int argc, _TCHAR* argv[])
{
	/******通过导入别的编译单元的头文件的方式来调用全局变量**********/
	g_INT=20;
	num=2+20;
	printf("定义、声明分别在.h .cpp文件中的全局变量调用结果:%d\n",num);

    return 0;
}

 

Note: When you define a static global variables, it is recommended to put it in the compilation unit need .cpp file rather than .h file, so do not give other information compilation unit causing unnecessary pollution. GT_staticDefineINT following variables code.

#include "StdAfx.h"
#include "B.h"


//22声明&定义静态全局变量
 static int gT_staticDefineINT=0; 
B::B(void)
{
	m_vable=0;
}


B::~B(void)
{
}
int B::UseGlobalVariable(int a,int b)//22调用全局变量
{
	int c=0;
	c=a+b+g_INT;
	return c;
}

Here is the reason why so many recommendations, combined with the two predecessors of the article, summarized as follows: 

The proposed grounds

Learn the basics

(1) coding unit (block)

  Finished writing code on VC or VS, when you click the button ready to generate compiled exe file, the compiler do the work of two steps:

  The first step, each .cpp (.c) and the corresponding documents translated into obj .h file;

  The second step, all the works carried out obj files LINK, generate the final .exe file.

 

  So, errors may occur in two places:

  A compile-time error, this is mainly grammatical errors;

  A, when the link error, repeat mostly defined variables.

    

  Each coding unit means obj file generated at compile time.

  A obj file is a compilation unit.

  A .cpp (.c) and its corresponding .h files compiled together to form a unit.

  A project by a number of compilation units, each obj file contains a relative address variable storage and so on.


(2) statements and definitions

    When you declare a function or variable, it does not give the actual physical memory space, it sometimes can ensure your program is compiled by;

    When a function or variable is defined, it has an actual physical space in memory.

    If your external variables referenced in the compilation unit is not defined anywhere in the whole project, then even if it can be compiled by also being given at the time of connection, because the program can not find this variable in memory.

    Function or variable can be declared many times, but is defined only once.

 

(3) extern role

    A role: when it is used in conjunction with a "C", as extern "C" void fun (int a, int b) ;, the compiler when compiling the function name fun C in accordance with the rules to translate the corresponding function name and instead of C ++.

    Role two: when it is not the "C" with modified variable or function, such as in the header file, extern int g_nNum ;, its role is to declare a function or variable scope keyword, its functions and variables declared It can be used in the compilation unit or other compilation units.

    I.e. compilation unit B to reference global variables or functions defined in the compilation unit A, B comprises a long compilation unit header file compilation unit A can, at compile time, the compiler unit B can not be found, although the function or variable, but it no error, it generates object code from a compilation unit at link found in this function.

The difference between static global variables and global variables

Global variables itself is static storage mode, static global variables of course, a static storage. This is no different in the two storage methods. Except that the scope of the non-static global variable is the entire source program, when a plurality of source files from the source code, non-static global variable in each source file is valid. The static global variables are restricted in their scope, that is only valid within the definition of the variable source file, it can not be used in other source files in the same source program. Due to the limited scope static global variables within a source file, can only function in the source file for the public, so to avoid causing errors in the other source files.

static modification of the scope of global variables can only be a compilation unit itself. When other compilation units use it, simply copy the value to other compilation units, other compilation units will open a further memory to hold it, in other compilation units changing it does not affect the definition of the value itself. That is when other compilation unit A uses it, what physical address, other compilation units B and use it, what physical address is not the same, its modifications A and B are not transmitted to the other party.

Note: The general definition of static global variables, are taking it in the .cpp file rather than .h file, so do not give other information compilation unit causing unnecessary pollution.

 

These are summarized their insights and, if wrong, please correct me, a total of progress.

Guess you like

Origin blog.csdn.net/ZDT_zdh/article/details/90059753