C ++包含标头问题 [英] C++ include header problem

查看:55
本文介绍了C ++包含标头问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c/c ++的新手,我对以下内容感到困惑:

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

  1. 我是否应该将类声明放在其自己的头文件中,而将实际实现放在另一个文件中?
  2. 我是否应该将诸如< iostream> 之类的标头放在example.h文件或example.cpp文件中?
  3. 如果所有类都需要使用< iostream> ,并且我将一个类的头文件包含在另一个类的头中,这是否意味着我包括了< iostream> 两次?
  4. 如果我使用很多STL类,那么使用 std :: 的好习惯是什么?
  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.是否应该将类声明放在其自己的头文件中,和实际的实现在另一个文件?

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

如果要处理模板,则可以在同一头文件中分别编写类的定义和类成员的定义.另外,如果要使成员内联,则可以在类定义本身内对其进行定义.在任何其他情况下,最好将类的定义(.hpp文件)和类的成员的定义(.cpp)分开.

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.是否应将标题放在example.h文件或example.cpp中文件?

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

这取决于您是否仅在example.h文件或.cpp文件中需要这些标头.

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

3.如果所有类都需要使用,并且我将类的头文件包含在其中另一个班级的标题,是否表示我包含两次?

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.如果我使用很多STL类,那么使用std ::?有什么好习惯?

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

我认为最好是每次都可以使用 std :: 而不是使用命名空间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).

但最重要的是,问题4到底在哪里?

But most importantly where is question number 4 anyway?

这篇关于C ++包含标头问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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