Spitfire original, 9CBS startup, repost, please explain, thank you.
Welcome everyone to discuss the problem http://www.allaboutprogram.com/bb/index.php
c) Class definition There is always an internal connection, not the Inline member function definition There are always external connections, regardless of this member function is static, virtual or general member functions, class static data member definitions total external connections.
1. Definition of class has internal connections. If not, imagine that INCLUDE in 4 CPP files defines the header file in the 4 compilation units, there are external connections, and it will be wrong when the connection is connected.
Look at the example below:
//main.cpp
Class B // Definition, internal connection
{
Static int S_i; // Static class member statement, 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 Members 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 B;
D D;
Return 0;
}
//a.cpp
Class B
{
INT K;
}
Struct D
{
INT D;
}
In this example, main.cpp has the definition of Class B and Class D in the main.cpp, but does not take a link error when compiling both CPP files.
2. Class of non-inline member functions (general, static, virtual all) There are always external connections, so when you include a class's header file, use this class's function, you can connect to the correct class member function. On, continue with the above as an example, if it changes Struct D in A.CPP to
Struct D // 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;
}
At this time, main.cpp has external connections in the D:: foo in A.CPP, and Multiply Defined Symbols will appear in connection.
3. The static data members of the class have external connections, such as B :: S_i in the above example, so when you define a static data member in main.cpp, other compilers use B :: S_i, it will connect to Main. .CPP corresponds to the S_i of the compile unit.
d) Inline functions There is always an internal connection, no matter what function is
// main.cpp
Inline int foo () {return 1;} // Inline global function, internal connection
Class Bar // 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, the Base's Inline function is defined in base.cpp, then in main.cpp, INCLUDE "base.h" compiles do not have problems, but will be connected Can't find the function K, so the class's Inline function is best placed in the header file, so that each CPP containing the header file can find the inline function.
Now there is a understanding of the connection in C , it can be clearly known that the reason is incorrect when it produces a connection. When you create an error that is not connected, this shows that all compilers do not have external connections of this entity; when you find multiple connection entities, this shows that there are multiple compilers to provide the same name. External connection entity. At the same time, when the programming is performed, it should also be noted that there is not an external connection with the functions, classes, variables of this compilation unit, and reduce the connection conflict with other compiletics.
However, there is no explanation of the Template function and the connection between the Template function and the Template Class here, and there is no explanation for some special conditions (such as the inline function can't be inline), how to know the matter, and listen to the decomposition :).