VC + + define global variables and extern usage

Source: Internet
Author: User
Tags variable scope

Basic explanation: extern can be placed before a variable or function to mark the definition of a variable or function in another file, prompting the compiler to look for its definition in other modules when encountering this variable and function. Also extern can be used for link designation.

That is, the extern has two functions, the first, when it is in conjunction with "C", such as: extern "C" void fun (int a, int b); then tell the compiler to compile the function name of fun by the C rule to translate the corresponding function name instead of C + + The rules in the translation of this function name will be the name of the fun to become unrecognizable, may be [email protected]_int_int#%$ may also be other, this depends on the compiler's "temper" (different compiler uses the same method), why do it, because C + + Support functions of the overloaded Ah, here do not go too much to discuss the problem, if you are interested can go online search, I believe you can get a satisfactory explanation!
Second, when extern does not modify the variable or function together with "C", as in the header file: extern int g_int; Its function is to declare the function or global variable scope of the keyword, its declared functions and variables can be used in other modules of this module, remember that it is a declaration is not a definition! That is, if the B module (the compilation unit) refers to a global variable or function defined in a module (compilation unit), As long as it contains the header file of the A module, in the compilation phase, module B, although it cannot find the function or variable, but it does not error, it will be in the connection when the module a generated from the target code to find this function.

VC defines global variables and extern usage: Global variables are generally defined as: 1 int  int  myInt; So it can be used.  2. Add:int myInt in stdafx.cpp, then add in stdafx.h:externint  Myint is so defined that it is visible in whatever file.

VC definition Global Variables and extern usage

Global variables are generally defined like this:
1. Define int myInt in a class of. cpp, and then in the. cpp of the place to be used, the extern int myInt.
2. Add: int myInt in Stdafx.cpp, then add in stdafx.h: extern int myInt
3. First define a Glbs.h and put all the global variables in the original definition. Then define a Externs.h to add extern to the variables previously defined in Glbs.h. Note: If you set the initial value in Glbs.h, do not add values in the Externs.h. Then called when the first call to the
# include <glbs.h>, later called # include <Externs.h>
4. Defining variables in the CAPP: CString g_suser;//defining the Global
This is true in the class to be referenced: extern csvapp theapp;//joins this sentence. CPP file before the constructor.
When referencing: if (theapp.m_susername = = "");//This reference.
5. If you want to use a custom global custom class, you can make it available in all files, typically as follows.
First, add the header file of the class in the StdAfx.h
Second, the definition of class is added in Stdafx.cpp such as: CMyClass G_class;
Third, in your program's app class, if the header file of the Ctestprojectapp class is preceded by an extern cmyclass G_clas.

VC + + define global variables and extern usage

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.