头文件包含/转发声明 [英] Header files inclusion / Forward declaration
本文介绍了头文件包含/转发声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的C ++项目中,我必须使用包含( #includemyclass.h
)的头文件吗?当我必须使用类的前向声明( class CMyClass;
)?
解决方案
作为规则,首先尝试前向声明。这将减少编译时间等。如果那不编译去 #include
。如果您需要执行以下任何操作,则必须执行#include:
- 访问类的成员或函数。 / li>
- 使用指针运算。
- 使用sizeof。
- 任何RTTI信息。
-
新
/删除
,复制等。 - 继承自
-
- 将其作为成员。
- 。
(6,7,8,9来自@Mooing Duck)
<他们可能更多,但我今天没有我的语言法律帽子。
In my C++ project when do I have to use inclusion (#include "myclass.h"
) of header files? And when do I have to use forward declaration of the class (class CMyClass;
)?
解决方案
As a rule try the forward declaration first. This will reduce compile times etc. If that doesn't compile go for the #include
. You have to go for the #include if you need to do any of the following:
- Access a member or function of the class.
- Use pointer arithmetic.
- Use sizeof.
- Any RTTI information.
new
/delete
, copy etc.- Use it by value.
- Inherit from it.
- Have it as a member.
- Instance in a function.
(6,7,8,9 from @Mooing Duck)
They're are probably more but I haven't got my language law hat on today.
这篇关于头文件包含/转发声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文