我对 C++ 相当陌生,但我的理解是,#include 语句本质上只是将#included 文件的内容转储到该语句的位置。这意味着如果我的头文件中有许多'#include'和'using'语句,我的实现文件可以只#include头文件,如果我不重复其他语句,编译器不会介意.
人呢?
我主要担心的是,如果我不重复“#include”、“using”和“typedef”(现在我想到了)语句,它会将这些信息从使用它的文件中删除,这可能导致混乱。
我现在只是在做一些小项目,它不会真正引起任何问题,但我可以想象,在有更多人参与的大型项目中,它可能会成为一个重大问题。
一个例子如下:
更新:我的“Unit”函数原型在其返回类型和参数中包含字符串、ostream 和 StringSet - 我的头文件中没有包含仅在实现文件中使用的任何内容。
//Unit.h
#include <string>
#include <ostream>
#include "StringSet.h"
using std::string;
using std::ostream;
class Unit {
public:
//public members with string, ostream and StringSet
//in their return values/parameter lists
private:
//private members
//unrelated side-question: should private members
//even be included in the header file?
} ;
//Unit.cpp
#include "Unit.h"
//The following are all redundant from a compiler perspective:
#include <string>
#include <ostream>
#include "StringSet.h"
using std::string;
using std::ostream;
//implementation goes here