Resolve the internal connection and external connection in C ++

Source: Internet
Author: User
Tags constant definition
1. Statement

A declaration introduces a name into a scope;
In C ++, it is legal to repeat a declaration in a scope.
The following are declarations:

Int Foo (INT, INT); // function pre-declaration
Typedef int; // typedef Declaration
Class bar; // class pre-declaration
Extern int g_var; // external reference Declaration
Class bar; // class pre-declaration
Typedef int; // typedef Declaration
Extern int g_var; // external reference Declaration
Friend test; // member statement
Using STD: cout; // namespace reference Declaration

You can repeat these statements multiple times in the same scope.
There are two types of declarations that cannot be repeated. They are the declarations of class member functions and static data members.

Class foo
{
Typedef static int I;
Typedef static int I; // No
Public:
Int Foo ();
Int Foo (); // No
};

2. Definition

A definition provides a unique description of an object (type, instance, function) in a scope.
An object cannot be defined repeatedly in the same scope.
The following are definitions.

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;
Bar B;

3. compilation unit

When a C or CPP file is being compiled, the Preprocessor first recursively contains the header file to form a single source file containing all necessary information. This source file is a compilation unit. This compilation unit will be compiled into a target file (. O or. OBJ) with the same name as the CPP file name ). The Connection Program Associates the symbols generated in different compilation units to form an executable program.

4. Free Functions

If a function is a free function, it is neither a member function of the class nor a friend function.

Next let's take a lookInternal ConnectionAndExternal Connection
Internal Connection:
If a name is local for its compilation unit and does not conflict with the same name in other compilation units during the connection, the name has an internal connection (note: sometimes the declaration is considered as connectionless. Here we regard it as an internal connection ).
Internal connections are available in the following cases:
A) All statements
B) definitions of static free functions, static friends functions, and static variables in namespace (including global namespace)
C) Enum Definition
D) Inline Function Definition (including free and non-free functions)
E) Class Definition
F) const constant definition in namespace
G) Definition of union
  External Connection:In a multi-file program, if a name can interact with other compilation units during connection, this name has an external connection.
External connections are available in the following cases:
A) Class non-inline functions always have external connections. Including class member functions and class static member functions
B) static member variables always have external connections.
C) non-static free functions, non-static friends functions, and non-static variables in the namespace (including the global namespace)

Example: a) the declaration, Enum definition, and Union definition have internal connections.

All declarations, Enum definitions, and Union definitions do not generate connection symbols after compilation, that is to say, declarations with the same name and definitions of Enum and union in different compilation units do not encounter errors with multiple symbols during connection.

// Main. cpp
Typedef int; // typedef declaration, internal connection
Enum color {red}; // Enum definition, internal connection
Union x // Union definition, internal connection
{
Long;
Char B [10];
};
Int main (void)
{
Int I = red;
Return I;
}
// A. cpp
Typedef int; // declare an int type alias in A. cpp. No error will occur during connection.
Enum color {blue}; // defines an Enum color in A. cpp. No error will occur during connection.
Const int I = blue; // const constant definition, internal connection
Union x // Union definition, internal connection
{
Long;
Char B [10];
};

B) The static free functions, static friends functions, static variables, and const constant definitions in the namespace have internal connections.

// Main. cpp
Namespace Test
{
Int Foo (); // function declaration, internal connection
Static int I = 0; // namespace static variable definition, internal connection
Static int Foo () {return 0 ;}// namespace 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 const constant definition, internal connection
Int main (void)
{
Return 0;
}

// A. cpp
Namespace Test
{
Int I = 0; // namespace 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

In the global namespace, Main. cpp defines static variables I, constants K, AND STATIC FREE functions Foo, which have internal connections. If you remove the static or const modifiers of these variables or functions, the multiply defined symbols error will occur during the connection. they conflict with the global variables and global functions in A. cpp.

C) class definitions always have internal connections, and non-inline class member functions always have external connections, whether the member functions are static, virtual, or general member functions, class static data member definitions always have external connections.

1. The class definition has an internal connection. If not, imagine that you have defined the class base header file in the four CPP files, and all the class base files in the four compilation units have external connections, an error occurs during connection.
See the following example:

// Main. cpp
Class B // class definition, internal connection
{
Static int s_ I; // static class member declaration, internal connection
Public:
Void Foo () {++ s_ I;} // class inline function, internal connection
};
Struct d
{
Void Foo (); // class member function declaration, internal connection
};
Int B: s_ I = 0; // class static data member definition, external connection
Void D: Foo () // class member function definition, external connection
{
Cout <"D: Foo in Main. cpp" <Endl;
}
Int main () // main function, global free function, external connection
{
B;
D;
Return 0;
}
// A. cpp
Class B
{
Int K;
};
Struct d
{
Int D;
};

In this example, both main. cpp and A. cpp have definitions of class B and classd, but the link error does not occur when compiling the two CPP files.
  2. non-inline member functions of the class (generally static or virtual) always have external connections. When you include a Class header file and use this class function, you can connect to the correct class member function.Continue with the above as an example. If you change struct D in A. cpp

Struct D // class definition
{
Int D;
Void Foo (); // class member function declaration
};
Void D: Foo () // class member function definition, external connection
{
Cout <"D: Foo in A. cpp" <Endl;
}

In this case, both main. cpp and D: Foo in A. cpp have external connections, and the multiply defined symbols error will occur during the connection.

  3. static data members of the class have external connections.In the preceding example, B: s_ I. CPP defines static data members. If other compilation units use B: s_ I, they are connected to main. CPP corresponds to s_ I of the compilation unit.

D) inline functions always have internal connections, no matter what function this function is.

// Main. cpp
Inline int Foo () {return 1;} // inline global function, internal connection
Class bar // class definition, internal connection
{
Public:
Static int F () {return 2;} // inline class static function, internal connection
Int g (int I) {return I;} // inline class member function, internal connection
};
Class base
{
Public:
Inline int K (); // class member function declaration, internal connection
};
Inline int base: K () {return 5;} // inline class member function, internal connection
Int main (void)
{
Return 0;
}

If your base class is defined in base. h, while the base inline function is in base. as defined in CPP. include "base. H "compilation will not cause problems, but function K will not be found during connection, so the inline function of the class is best placed in the header file, so that every CPP containing the header file can find the inline function.
Now I have an understanding of the connection in C ++ and can clearly understand the cause of the connection error. When you create a connection error, it means that all compilation units do not have an external connection to the object. When you connect, multiple connected entities exist, this indicates that multiple compilation units provide external connected entities with the same name. At the same time, when designing a program, be sure not to allow external connections to functions, classes, variables, and other functions used by the current compilation unit to reduce connection conflicts with other compilation units.
However, the connectivity between the template function and the templateclass function is not described here, and some special cases are not described (for example, the inline function cannot be inline ).

Conversion from http://blog.csdn.net/mood8125/article/details/1439904

 

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.