Resolve the internal connection and external connection in C ++

Source: Internet
Author: User
Tags constant definition

In IntroductionC ++Before the internal connection and external connection in, describe some concepts.

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:

 
 
  1. Int foo (int, int); // function pre-declaration
  2. Typedef int Int; // typedef Declaration
  3. Class bar; // class pre-declaration
  4. Extern int g_var; // external reference Declaration
  5. Class bar; // class pre-declaration
  6. Typedef int Int; // typedef Declaration
  7. Extern int g_var; // external reference Declaration
  8. Friend test; // member statement
  9. Using std: cout; // namespace reference Declaration
  10. Friend test; // member statement
  11. Using std: cout; // namespace reference Declaration
  12. Int foo (int, int); // function pre-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.

 
 
  1. Class foo
  2. {
  3. Static int I;
  4. Static int I; // No
  5. Public:
  6. Int foo ();
  7. Int foo (); // No
  8. };

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.

 
 
  1. int y;  
  2. class foo {...};  
  3. struct bar {...};  
  4. foo* p;  
  5. static int i;  
  6. enum Color{RED,GREEN,BLUE};  
  7. const double PI = 3.1415;  
  8. union Rep{...};  
  9. void test(int p) {};  
  10. foo a;  
  11. 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.

The following describes 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 compilation units, the name has an internal connection (Note: Sometimes the declaration is considered as connectionless, and here we are regarded 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 the namespace (including the 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)

The following is an example:

A) Declarations, enum definitions, and union definitions 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.

 
 
  1. // Main. cpp
  2. Typedef int Int; // typedef declaration, internal connection
  3. Enum Color {red}; // enum definition, internal connection
  4. Union X // union definition, internal connection
  5. {
  6. Long;
  7. Char B [10];
  8. };
  9. Int main (void)
  10. {
  11. Int I = red;
  12. Return I;
  13. }
  14. // A. cpp
  15. Typedef int Int; // declare an int type alias in a. cpp. No error will occur during connection.
  16. Enum Color {blue}; // defines an enum Color in a. cpp. No error will occur during connection.
  17. Const Int I = blue; // const constant definition, internal connection
  18. Union X // union definition, internal connection
  19. {
  20. Long;
  21. Char B [10];
  22. };

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

 
 
  1. // Main. cpp
  2. Namespace test
  3. {
  4. Int foo (); // function declaration, internal connection
  5. Static int I = 0; // namespace static variable definition, internal connection
  6. Static int foo () {return 0 ;}// namespace static function definition, internal connection
  7. }
  8. Static int I = 0; // Global static variable definition, internal connection
  9. Static int foo () {return 1 ;}// Global static function definition, internal connection
  10. Const int k = 0; // global const constant definition, internal connection
  11. Int main (void)
  12. {
  13. Return 0;
  14. }
  15. // A. cpp
  16. Namespace test
  17. {
  18. Int I = 0; // namespace variable definition, external connection
  19. Int foo () {return 0;} // namespace Function Definition, external connection
  20. }
  21. Int I = 0; // global variable definition, external connection
  22. Int k = 0; // global variable definition, external connection
  23. 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:

 
 
  1. // Main. cpp
  2. Class B // class definition, internal connection
  3. {
  4. Static int s_ I; // static class member declaration, internal connection
  5. Public:
  6. Void foo () {++ s_ I;} // class inline function, internal connection
  7. };
  8. Struct D
  9. {
  10. Void foo (); // class member function declaration, internal connection
  11. };
  12. Int B: s_ I = 0; // class static data member definition, external connection
  13. Void D: foo () // class member function definition, external connection
  14. {
  15. Cout <"D: foo in main. cpp" <endl;
  16. }
  17. Int main () // main function, global free function, external connection
  18. {
  19. B B;
  20. D;
  21. Return 0;
  22. }
  23. // A. cpp
  24. Class B
  25. {
  26. Int k;
  27. };
  28. Struct D
  29. {
  30. Int d;
  31. };

In this example, both main. cpp and a. cpp have definitions of class B and class D, but the link error does not occur when compiling the two cpp files.

2. non-inline member functions of a class (generally static or virtual) always have external connections. When you include a Class header file and use this class function, connect to the correct class member function. Continue with the above example. change struct D in cpp

 
 
  1. Struct D // class definition
  2. {
  3. Int d;
  4. Void foo (); // class member function declaration
  5. };
  6. Void D: foo () // class member function definition, external connection
  7. {
  8. Cout <"D: foo in a. cpp" <endl;
  9. }

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. the static data member of the class has an external connection. In the example above, 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.

 
 
  1. // Main. cpp
  2. Inline int foo () {return 1;} // inline global function, internal connection
  3. Class Bar // class definition, internal connection
  4. {
  5. Public:
  6. Static int f () {return 2;} // inline class static function, internal connection
  7. Int g (int I) {return I;} // inline class member function, internal connection
  8. };
  9. Class Base
  10. {
  11. Public:
  12. Inline int k (); // class member function declaration, internal connection
  13. };
  14. Inline int Base: k () {return 5;} // inline class member function, internal connection
  15. Int main (void)
  16. {
  17. Return 0;
  18. }

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 template class is not described here, and some special situations are not described (for example, the inline function cannot be called inline ).

Related Article

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.