开发者

C++ include header problem

I am new to c/c++, I am confused about followings:

  1. Whether I should put class declarations in its own header file, 开发者_运维知识库and actual implementation in another file?
  2. Whether I should put headers like <iostream> in the example.h file or in example.cpp file?
  3. If all the classes need to use <iostream>, and I include a class's header file into another class's header, does it mean I included <iostream> twice?
  4. If I use a lot STL classes, what is a good practice to use std::?


1.Whether I should put class declarations in its own header file, and actual implementation in another file?

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

2.Whether I should put headers like in the example.h file or in example.cpp file?

It Depends on if you need those headers in the example.h file or in your .cpp file only.

3.If all the classes need to use , and I include a class's header file into another class's header, does it mean I included twice?

It happens if you don't wrap your class definitions by the following macros:

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

5.If I use a lot STL classes, what is a good practice to use std::?

I think it is better whenever you can to use std:: each time instead of using namespace std. This way you will use only 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?


  1. Generally, yes. It helps with organization. However, on small projects it may not be that big of a deal.

  2. I'm having trouble understanding the question here. If you're asking where to put the #include directive, the implementation file should include the header file.

  3. Yes, but the use of include guards prevents multiple inclusions.


  1. You normally should, but for relatively small projects you may avoid having an implementation file as much as possible;
  2. If your header is using only incomplete types from the <iostream>, you can avoid including it, but you'll need forward declarations for these types (see When to use forward declaration?). Yet, for simplicity, if the type uses template, I normally include the respective header;
  3. No. The include guards guarantee that a header is included only once in the same translation unit;
  4. A common good practice is to not put using namespace std in a header file. Be aware of namespace conflicts too;


Typically, you do put class declarations (including declarations of members) into header files and definitions of member functions (methods) in source files. Headers usually have names like *.h or *.hpp. As to point 3, you should put include guards into your headers so they can be safely included multiple times in the same source file; then you can include them everywhere you need them. I don't understand point #5: are you asking about when to use std:: namespace qualification?


For the "included twice" problem, here is a common pattern 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. As long as they're not templates, generally yes. Templates (for better or worse) have to be put in headers.
  2. I prefer to make each of my headers "standalone", so if any other header is needed for it to function, it includes that header itself (e.g., if I have a class that uses std::string, the header for that class will #include <string>.
  3. No. With a few special exceptions, the standard headers are required to be written so you can include them more than once without it changing anything (the primary exception is assert.h/cassert, which it can make sense to include more than once).
  4. I'm not sure exactly what you're asking. If you're asking about a using directive like using namespace std;, then it's generally (though certainly not universally) disliked. A using declaration like using std::vector; is generally considered less problematic.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜