C ++ includes a header problem

I am new to c / C ++, I am confused about the following:

  • Should I put class declarations in my own header file and the actual implementation in another file?
  • Should I put headers of type <iostream> in the example.h file or in the example.cpp file?
  • If all classes should use <iostream> , and I include the class header file in another class header, does this mean that I have included <iostream> twice?
  • If I use many STL classes, should I use std:: ?
+7
source share
6 answers

1. If I have to put class declarations in my own header file, and the actual implementation in another file?

You can write the class definition and the definition of class members separately in the same header file if you are manipulating templates. Also, if you want your members to function in a string, you can define them inside the class definition itself. In any other case, it is better to separate the class definition (.hpp file) and the class member definition (.cpp).

2. If I have to put the headers, as in the example.h file, or in the example.cpp file?

It depends on whether you need these headers in the example.h file or only in your .cpp file.

3.If all classes should use, and I include the class header file in another class header, does this mean that I am included twice?

This happens if you do not complete the definition of your class with the following macros:

 #ifndef FOO_HPP #define FOO_HPP class { ... }; #endif 

5.If I use many STL classes, what is good practice for using std ::?

I think it's better when you can use std:: every time instead of using namespace std . This way you will only use the namespaces that you need, and your code will be more readable because you will avoid namespace conflicts (imagine two methods that have the same name and belong to two different namespaces).

But most importantly, where is question number 4 anyway?

+8
source
  • Generally yes. It helps the organization. However, on small projects this may not be so much.

  • I have a problem understanding the question here. If you ask where to put the #include directive, the implementation file should include a header file.

  • Yes, but using include guards prevents multiple inclusions.

+2
source
  • Usually you should, but for relatively small projects, you can avoid the implementation file as much as possible;
  • If your header uses only incomplete types from <iostream> , you may not include it, but you will need advanced declarations for these types (see When to use forward declaration? ). However, for simplicity, if the type uses a template, I usually include the appropriate header;
  • Not. enable guards ensure that the header will be included only once in the same block;
  • A common good practice is not to place using namespace std in the header file. Also be aware of namespace conflicts;
+2
source

Typically, you place class declarations (including member declarations) in header files and definitions of member functions (methods) in the source files. Headers usually have names like *.h or *.hpp . With regard to paragraph 3, you should include guards in your headers so that they can be safely included several times in the same source file; then you can include them wherever you need them. I donโ€™t understand point number 5: you ask when to use the std:: namespace qualification?

+1
source

For the "double included" problem, here is a general template for your header files:

 // _BLAHCLASS_H_ should be different for each header, otherwise things will Go Bad. #ifndef _BLAHCLASS_H_ #define _BLAHCLASS_H_ ... rest of header ... #endif 
+1
source
  • As long as they are not templates, usually yes. Templates (better or worse) should be placed in the headers.
  • I prefer that each of my headings be "standalone", so if it needs any other heading, it includes that heading itself (for example, if I have a class that uses std::string , the heading for this class will be #include <string> .
  • Not. With some special exceptions, standard headers must be written, so you can include them more than once without changing anything (the main exception is assert.h / cassert , which may make sense to include more than once).
  • I'm not sure what you are asking. If you ask about using directives like using namespace std; , then she generally (although, of course, not universally) do not like it. Using type declarations using std::vector; usually considered less problematic.
+1
source

All Articles