How to write a C ++ class (a) to prevent the duplicate file header contains

  Today, I use a simple theme, write a C ++ class that begins as I wrote in the blog garden first essays of it.

  "How to write a C ++ class." This theme is not too small, write a class, who will not ah. Uh ,,, I found writing a C ++ class is very simple, but many people have written bad, especially those just out of school and training students to write out of class, appalling. . . (Actually, when bloggers just out of school to do, ha ha, no discrimination, but we have a growing process) I thought this topic is relatively small, that is, up to a few articles of thing, but began to write, the idea of ​​it, no ten articles, really lots to talk about. . . (What, with a very, very long article finished? Forget it, I'm not that masochistic tendencies, but also no time ah, to go to work, to enhance their own ...)

  Well, without further ado, the first on a simple class, look at the code first is the header file person.h (By the way, I just want to create a person, after all, hard to think of examples, but the examples I usually can not work with, those are trade secrets, fear of veterans came to the door, it is awkward ...):

 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

  Then the file is person.cpp

 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 }

  Then the executable file main.cpp

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

  OK! Next to compile and run it!

 

   it is good! First, from the most simple to start, in the header file person.h file Why add # ifndef, # define pre-compiled and #endif statements it? Without so what?

  Let me talk about the reasons, to give an example, the reason is to prevent duplicate header file contains, # ifndef _Person_H_ it means if not defined _Person_H_ macro, macro to #define _Person_H_ defined _Person_H_, once you have this macro source file then include the header, they will not enter the range #ifndef, define the same variable repeated but runs directly from the back #endif, of course, no code behind the #endif, this document can be ignored, the following two examples to now!

  First, the original person.h removed in a prepared statement.

 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

  Comprising person.h then repeated in the header file main.cpp

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

  Compile the results:

 

  See, compile the results error, repeat the definition, of course, we removed the comment, restore the original precompiled headers, even if repeated includes the header files, duplicate definition errors will not appear.

  Of course, some people will say, "I was not so stupid, in the same header file that contains the same file twice it", so ah, if you include the header file in a different file twice it? See the following example.

  Test.h add a new file, just add #include "person.h" a statement:

1 #ifndef _TEST_H   
2 #define _TEST_H   
3                      
4 #include "person.h"                                                             
5                      
6 #endif 

  Then main.cpp has test.h file:

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

  Compile:

 

   It is wrong repeat it contains.

  Perhaps some people argue, and I never add this, or some of the documents without, there is no problem ah. I just want to throw you one, it's because you write the code too simple. . . I work everyday, are very common G on a C ++ project code, so be very focused on the details of the program, to develop good habits, or else go wrong, can find a bug dead, ha ha. . .

Guess you like

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