C + + program file links

Source: Internet
Author: User

1. What is a conversion unit
Each. cpp file in the program and all header files it contains are called a conversion unit.
The compiler processes each conversion unit, generates a binary object file, and identifies it with the extension. obj.
The linker combines the object files to generate an executable file identified with the extension. exe.

2. Scope
Use {} to limit the scope of a variable

An internal scope can define a name that is the same as an external scope, at which point the name defined in the internal scope hides the name in the outer scope.

To access the names in the outer scope, you can use the parse operator::, an introduction to the parse operator, which can be referenced by the C + + scope resolution Operator:: Use

intMain () {Const intLimit =Ten; Std::cout<<"Limit 1 is"<<limit<<Std::endl; {Const intLimit =5; Std::cout<<"Limit 2 is"<<limit<<Std::endl;std::cout<<"Limit 3 is"<<::limit<<Std::endl;} Std::cout<<"Limit 4 is"<<limit<Std::endl;}

Output Result:
Limit 1 is 10
Limit 2 is 5
Limit 3 is 10
Limit 4 is 10

3. The global Name property of the conversion unit linkage
The global name in each conversion unit has an attribute linkagethat indicates where the global name can be used in the program code.
Internal Link Properties : The name can only be accessed from anywhere in the same conversion unit. such as Global const variables .
External Link Properties : This name can also be accessed in other conversion units in addition to the same conversion unit access. In addition to the global const variable, the other global names are external link properties.

The local name does not have a link property.

4. What is a "one definition" rule
In all conversion units, the global names of external link properties, such as variables, functions, class types, enumeration types, and templates, can be defined only once .
In addition to inline functions, the definition of an inline function must appear in each transformation unit that invokes the function.

The name of the internal link property can be defined at the same time in multiple conversion units.

5. How to access a variable defined in another conversion unit
For a function, if the call and definition of a function are not in the same conversion unit, the compiler will mark the function call as external and let the linker handle it.

for variables, it's different. You must declare the variable by using the extern keyword . Represents the definition of the variable in another conversion unit.

The sample code is as follows:

Myextern.cpp int  the ; Mymain.cpp int Main () {    externint  limit;    Std::cout<<""<<limit<<Std::endl;     return 0 ;}

Introduced by the Link property linkage, we know that the const variable is an intrinsic link property and can only be accessed within the conversion unit.

Instead of defining a const variable, it is common to expect other conversion units to be available, such as Pi Pi,const double pi=3.14159265,

So how do you get the const variable to have an external link property as well?
We just need to add the extern keyword when defining a const variable.

The sample code is as follows:

Myextern.cpp extern Const double pi=3.14159265; mymain.cppint  main () {    extern  Constdouble  pi;    Std::cout<<""<<pi<<Std::endl;     return 0 ;}

It is also important to note that the location of the extern variable declaration determines the scope of the external variable.

Reference: "Introduction to C + + Classic Third Edition" pp.309-318

C + + program file links

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.