Internal connection and external connection (on)
Spitfire original, 9CBS startup, repost, please explain, thank you.
Welcome everyone to discuss the problem http://www.allaboutprogram.com/bb/index.php
In the first connection to the external connection, some concepts will be explained.
Declaration
A statement introduces a name into a scope;
In C , repeating a statement in a scope is legal.
The following is a statement:
INT foo (int, int); // function pre-declaration
Typedef int int; // typedef declaration
Class Bar; // Preferred statement
Extern Int g_var; // External reference declaration
Class Bar; // Preferred statement
Typedef int int; // typedef declaration
Extern Int g_var; // External reference declaration
Friend Test; // Friends Statement
Using std :: cout; // Name Space Reference Declaration
Friend Test; // Friends Statement
Using std :: cout; // Name Space Reference Declaration
INT foo (int, int); // function pre-declaration
You can repeat these statements many times in the same scope.
There are two statements that cannot be repeated, that is, the statement of class member functions and static data members.
Class foo
{
Static Int i;
Static Int i; //
PUBLIC:
INT foo ();
Int foo (); / /
}
2. Definition
One definition provides an entity (type, instance, function) uniquely description in one scope.
An entity is not repeated in the same scope.
The following is defined.
Int Y;
Class foo {...};
Struct bar {...};
Foo * p;
Static Int i;
ENUM color {red, green, blue};
Const Double Pi = 3.1415;
Union rep {...};
Void test (int P) {};
Foo a;
Bar B;
3. Compiling unit
When a C or CPP file is compiled, the preprocessor first recursively contains the header file, form a single source file containing all necessary information, this source file is a compilation unit. This compilation unit is compiled into a target file (.o or .Obj) with the same name with the CPP file name. The connection program associates the symbols generated in different compilation units to form an executable program.
4. Free function
If a function is a free function, then this function is not a member function of a class, nor is a friend function.
Look at the internal connection and external connection
Internal connection: If a name is partial for its compilation unit, and does not conflict with the same name in other compilers when the connection is connected, then this name has internal connection (Note: Sometimes it will also be regarded as the declaration It is not connected, here we are unified as an internal connection).
There is an internal connection:
a) all statements
b) Static free function, static friend function, static variable definition of name space (including global name space)
c) ENUM definition
d) Inline function definition (including free functions and non-free functions)
e) definition of class
f) CONST constant definition in the namespace
g) Definition of Union
External connection: In a multi-file program, if a name can interact with other compiletable units when a name is connected, then this name has an external connection.
There are external connections:
a) The class non-inline function always has an external connection. Includes class member function and class static member function B) class static member variables There are always external connections.
c) Non-static free functions, non-static friend functions and non-static variables in namespaces, including global namespaces.
Below examples:
a) declaration, enum definition, union definition has internal connection
All statements, enum definitions, and Union definitions do not generate connection symbols after compiling, that is, the declarations and enums of the same name in different compilation units, and Union definitions do not find multiple symbols errors when they are connected.
// main.cpp
Typedef int int; // typedef declaration, internal connection
ENUM color {red}; // enum definition, internal connection
Union X // Union definition, internal connection
{
Long a;
Char B [10];
}
Int main (void)
{
INT i = Red;
Return I;
}
// a.cpp
TYPEDEF INT INT; / / Declare an int type alias in A.CPP, do not errors when connected
Enum color {blue}; // Define a enum color in A.CPP, no errors occur when connecting
Const I = Blue; // Const Constant Definition, Internal Connection
Union X // Union definition, internal connection
{
Long a;
Char B [10];
}
b) Static free function, static friend function, static variable in the namespace, and Const constant definitions have internal connections
// main.cpp
Namespace Test
{
INT foo (); // function declaration, internal connection
Static int i = 0; // Name Space Static Variable Definition, Internal Connection
Static int foo () {return 0;} // Name Space Static function definition, internal connection
}
Static int i = 0; // global static variable definition, internal connection
Static int foo () {return 1;} // global static function definition, internal connection
Const Int K = 0; // Global Constanture Definition, internal connection
Int main (void)
{
Return 0;
}
//a.cpp
Namespace Test
{
INT i = 0; // Name spatial variable definition, external connection
INT foo () {return 0;} // namespace function definition, external connection
}
INT i = 0; // global variable definition, external connection
INT k = 0; // global variable definition, external connection
INT foo () {return 2;} // global function definition, external connection