应该在头文件和实现文件(C ++)中重复“#include”和“using”语句吗? [英] Should '#include' and 'using' statements be repeated in both header and implementation files (C++)?

查看:126
本文介绍了应该在头文件和实现文件(C ++)中重复“#include”和“using”语句吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C ++相当新,但我的理解是#include语句本质上只是将#included文件的内容转储到该语句的位置。这意味着如果我在头文件中有一些'#include'和'using'语句,我的实现文件可以#include头文件,编译器不介意如果我不重复其他语句



我们的主要担心是,如果我不重复'# include','using',以及'typedef'(现在我想到它)语句,它使用的信息远离它使用的文件,这可能会导致混乱。



我只是在小项目上工作,这不会真正导致任何问题,但我可以想象,在更大的项目,更多的人在工作,它可以成为一个重要的问题。



例如:



UPDATE:我的单位的函数原型有字符串, ostream和StringSet,它们的返回类型和参数 - 我不包括任何在我的头文件中只在实现文件中使用。

  // Unit.h 

#include< string>
#include< ostream>
#includeStringSet.h

使用std :: string;
using std :: ostream;

class Unit $ {

public:
// public成员with string,ostream和StringSet
//返回值/参数列表
private:
//私人成员
//无关的边问题:应该是私人成员
//是否包含在头文件中?
};


//Unit.cpp

#includeUnit.h

//以下都是冗余的透视:
#include< string>
#include< ostream>
#includeStringSet.h

使用std :: string;
using std :: ostream;

//实现在这里


解决方案

p>除非它包含在一个函数中,否则在标题中不应该有使用语句。这是坏的做法。如果有人在其头文件中包含使用mylib :: string 的行,如



至于 typedef 去...它不只是一个快捷方式,它具有语义意义。 typedef 定义一个类型,所以你真的不应该重新声明它。例如,这是一个错误:

  typedef int myint; 
typedef float myint;



因为冲突的类型,



重复 #include ,你应该考虑你是否真的需要首先将该文件包含在标题中。也许转发声明符合您的需求。


I'm fairly new to C++, but my understanding is that a #include statement will essentially just dump the contents of the #included file into the location of that statement. This means that if I have a number of '#include' and 'using' statements in my header file, my implementation file can just #include the header file, and the compiler won't mind if I don't repeat the other statements.

What about people though?

My main concern is that if I don't repeat the '#include', 'using', and also 'typedef' (now that I think of it) statements, it takes that information away from the file in which it's used, which could lead to confusion.

I am just working on small projects at the moment where it won't really cause any issues, but I can imagine that in larger projects with more people working on them it could become a significant issue.

An example follows:

UPDATE: my function prototypes for 'Unit' have string, ostream and StringSet among their return types and parameters - I am not including anything in my header file that is used only in the implementation file.

//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

解决方案

You should never have a using statement in a header unless it it contained within a function. It is bad practice. What if somebody else included a line like using mylib::string in their header file? Then you've got problems.

As far as typedef goes...it isn't just a shortcut, it has semantic meaning. typedef defines a type, so you really should never redeclare it. For example, this is an error:

typedef int   myint;
typedef float myint;

because of conflicting types.

For repeating #include, you should consider if you actually need to include the file in the header in the first place. Perhaps a forward declaration fits your needs.

这篇关于应该在头文件和实现文件(C ++)中重复“#include”和“using”语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆