How to write a C ++ class (B) is not recommended to directly include file containing cpp

  In many books have taught #include to include the header file, but did not teach why can not contain source files. . . So some students tried to directly contains the source file (OK, I admit, I have done this in college, and was lucky not to go wrong).

  That in the end should or should not contain source documents?

  I would like a contradiction, if included directly .cpp files, static variables that cpp file and the interface is not fully exposed to the outside .cpp file yet, as to static and effective interface in this document life cycle which have a limited wool? Does not directly contain the source file, a module can be divided according to the program file. (Many students might say, always said modular, you know blow these terms, do not feel what use is it, uh, then again, you write a little program) modular course, this benefits the temptation you can not, then do not give you compile, and you now impress it. See the following example, the practice of such direct .cpp file that contains the error could easily result in duplicate definitions.

  Or the original of that program as an example of it.

  person.h file

 1 #ifndef _Person_H
 2  #define _Person_H
 3     
 4 class Person  
 5  {   
 6 public:       
 7      Person(); 
 8      
 9     ~Person();
10                                                                              
11  private:      
12  };  
13      
14  #endif

  person.cpp file

 1 #include <iostream>                  
 2 #include "person.h"                  
 3            
 4 using std::cout;
 5 using std::endl;                                                            
 6            
 7 Person::Person()
 8 {          
 9     cout << "create a person" << endl;
10 }          
11            
12 Person::~Person()                    
13 {          
14     cout << "release a person" << endl;
15 }

   main.cpp file

1 #include <iostream>
2 #include "person.h"
3 using namespace std;
4 
5 int main()        
6 {                 
7     Person person;                                                          
8     return 0;     
9 }

  OK! This is "normal" in writing, ha ha, is not the problem. However, the results still look to run it.

 

   But if the direct #include contains ".cpp" file it, what results appear. See the following example.

  main.cpp file included directly person.cpp file:

 1 #include <iostream>
 2 #include "person.cpp"
 3                                                                                 
 4 using namespace std;
 5  
 6 int main()
 7 {
 8     Person person;
 9     return 0;
10 }

  Compile and run:

 

   There have been multiple definitions lead to not compile.

  In summary, we do not recommend directly contain .cpp file, even if there was unavoidable reasons, should not be written, for example, some people say a .cpp program content is too long, in such a way to extend the program (there is this person saying, you should re-design program into a program module), or to improve the coding efficiency (Chedan ah, can improve the efficiency of the compiler another said, now that the computer does not compile bad this time, do not use the machine the seventies and eighties it).

Guess you like

Origin www.cnblogs.com/qiuquanguan/p/11563820.html