The use of C + + global variables in multiple source code files

Source: Internet
Author: User

In larger projects, if you need to use global variables, you need to pay attention to some of the global variable declarations, improper use caused by problems.

This article has two main contents: Normal global variables, static global variables, global constants.

1. General Global variables: Suppose we need to use global variables to pass values in several different compilation units (such as two. cc files), as we have the following three source files:

main.cc: Run the portal, there is a main function, which will print out the value of the global variable var;

#include "def.h" #include  <iostream>using namespace Std;int main () {    cout<<var<<endl;    return 0;}

def.cc: The definition of the global variable var;

#include  "def.h" int var;    Can be assigned an initial value, or it can

Def.h: The declaration of the global variable var;

#ifndef  _def_h#define _def_hextern int var; #endif

Attention:

In the Linux compile time def.cc and main.cc both to compile (I write the time is to forget the compilation def.cc, silly to find main.cc compile error ... );

Declare global variables in. h with extern, define the global variable in a. cc file, and include the declared. h header file at the definition, so that the global variable is defined only once (otherwise, if it is defined in the. HT header file, it will encounter multiple definition errors at compile time), and then the other compilation unit of the global variable is used. C c file, just include the. h header file;

The extern declaration means that the declaration of the externally defined variable is introduced here, rather than declaring a local variable of the same name in this compilation unit;


2, static global variables: that is, using the static modified global variables, he can not use extern to introduce the declaration, that is, extern and static can not be used together, and the static global variable is very different from the normal global variables, we use a test program to illustrate;

Def.h: Defines the static global variable var and the header file of the fun function;

static  int  snum=22;static  int  sme;void fun ();

def.cc: The source file that defines the fun function;

#include "def.h" #include  <iostream>using namespace std;void fun () {    snum=33;    sme=3;    cout<<snum<< "," <<SME<<ENDL;}
Mod.h: Another header file that outputs the value of a global variable;

#include "def.h" #include <iostream>using namespace Std;void fun2 () {    cout<<snum<< "," << Sme<<endl;}

main.cc: Executive Portal;

#include "def.h" #include "mod.h" #include <iostream>using namespace Std;int main () {    cout<<snum< < "," <<sme<<endl;    Fun ();    Fun2 ();}

Output Result:

22,0

33,3

22,0


As you can see, the value printed in fun2 () does not change after the call of fun (), because the scope of the static modified global variable is only the compilation unit itself (the change takes effect in this compilation unit), and when used in other cells, the variable will have a new memory address, that is, Each compilation unit that uses it opens up a separate space for it and copies its initial values so that if it is modified in a cell, its value may not be the same in multiple compilation units;


Attention:

The static modification of the global variable declaration and definition is one, in the header file declared the static global variable, but also defined it, not as normal global variables are separate;

Multiple compilation units contain the header file where the static global variable resides, without causing a redefinition error, because each compilation unit opens up a new space to store it;


3. CONST GLOBAL variable: The const global variable is used in the same way as a normal global variable. Define and assign an initial value in. CC, declare it with extern in the. h header file, and then need to use the place that contains. h, where the memory address is also different in multiple compilation units (this is similar to the static global variable), but because it is a constant, the value cannot be modified, even if the memory address is not the same.


















The use of C + + global variables in multiple source code files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.