简体   繁体   English

我可以在.hpp文件中包含#include吗?

[英]May I #include in .hpp files?

I have a class called A, which has its .cpp and .hpp files. 我有一个名为A的类,它具有其.cpp.hpp文件。 It depends on classes B and C . 它取决于类BC Now, I'm wondering, should I #include B.hpp and #include C.hpp in A.cpp or A.hpp ? 现在,我想知道是否应该在A.cppA.hpp #include B.hpp#include C.hpp A.hpp I think it would be beneficial for me to #include in the A.hpp file, because I'm creating a vector of B* 's, and I'm not sure that the compiler will know the correct size of B if I only forward declare it (so I couldn't pointer increment correctly)... By "forward declaration", I mean writing class B; 我认为将#includeA.hpp文件中对我来说将是有益的,因为我正在创建B*的向量,并且我不确定如果仅当编译器知道B的正确大小时,向前声明它(所以我不能正确地增加指针)...“向前声明”是指编写class B; and class C; class C; in the beggining of A.hpp . A.hpp开始时。

As already mentioned, you can often work-around having to #include stuff by using forward declarations, and indeed the best practice is to minimize the amount of other headers included. 如前所述,您通常可以通过使用前向声明来#include东西,而实际上,最佳实践是尽量减少包含的其他标头的数量。

But: To answer your questions headline: yes you may, and the common mechanism to avoid "circular inclusion" is the following 但是:要回答您的问题标题:是的,可以,并且避免“循环包含”的常见机制如下

A.hpp: A.hpp:

#ifndef A_HPP_INCLUDED
#define A_HPP_INCLUDED

... // code

#endif

Now if say B.hpp includes A.hpp, and subsequently, C.hpp looks like: 现在,如果说B.hpp包括A.hpp,那么C.hpp看起来像:

#ifndef C_HPP_INCLUDED
#define C_HPP_INCLUDED

#include "A.hpp"
#include "B.hpp"

...
#endif

Then you won't get an error stating that eg 'class A' has already been defined. 这样,您将不会收到错误消息,说明例如已经定义了“ A类”。

If it's a vector of B* , you don't need to include B.hpp in the other header. 如果它是B*的向量,则无需在另一个标头中包含B.hpp A forward declaration is fine. 向前声明很好。 Pointers have the same size, regardless of what they point to. 指针大小相同,无论它们指向什么。

It's preferred to have includes only when necessary, and this certainly looks like it's not. 最好仅在必要时才包含,而看起来确实没有。

I don't know if i understand your class hierarchy, in my mind is like this: 我不知道我是否了解您的班级层次结构,在我看来是这样的:

B.hpp

class B
{
    B();
};

B.cpp

B::B() {};

C.hpp

class C
{
    C();
};

A.cpp

A::A() {};

A.hpp

class A
{
    A();
    B *b;
    C *C;
};

A.cpp

#include "B.hpp"
#include "C.hpp"

A::A() :
    b(NULL), c(NULL)
{
};

Is this correct? 这个对吗? (if not, please think about provide some code ;) If your class A have pointers to class B and class C a forward declaration must be the only thing you need. (如果没有,请考虑提供一些代码;)如果您的class A具有指向class B class Cclass C class A指针,则向前声明必须是您唯一需要的东西。

I'm not sure that the compiler will know the correct size of B if I only forward declare it 我不确定如果只向前声明它,编译器是否知道B的正确大小

Yes, as long as you include the B.hpp and C.hpp in the A.cpp file the compiler would be able to deduce its size (the class size, the pointer size is always the same). 是的,只要您在A.cpp文件中包含B.hppC.hpp ,编译器就可以推断出它的大小(类大小,指针大小始终相同)。 Why? 为什么? Just because in the cpp file it knows the correct size due the #include . 仅仅因为在cpp文件中,由于#include它知道正确的大小。 I've found this answer that would be useful to understand what I', trying to say. 我发现这个答案对理解我的意思很有帮助。

Would be fine in the hpp file instead? hpp文件中可以吗? Maybe, if your A.hpp would not be included in other files the class B and class C does not bother and spreading into another files. 也许,如果您的A.hpp不会包含在其他文件中,则class Bclass C不会打扰并传播到另一个文件中。 So, if it is the case that would not be neccessary. 因此,如果是这种情况,则不必要。 But IMHO the best practice is to forward declare and #include in the cpp files when it is possible. 但是恕我直言,最佳实践是在可能的情况下将声明和#include转发到cpp文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM