头文件包含/转发声明 [英] Header files inclusion / Forward declaration

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

问题描述

在我的C ++项目中,我必须使用包含( #includemyclass.h)的头文件吗?当我必须使用类的前向声明( class CMyClass; )?

解决方案

作为规则,首先尝试前向声明。这将减少编译时间等。如果那不编译去 #include 。如果您需要执行以下任何操作,则必须执行#include:


  1. 访问类的成员或函数。 / li>
  2. 使用指针运算。

  3. 使用sizeof。

  4. 任何RTTI信息。

  5. / 删除,复制等。

  6. 继承自

  7. 将其作为成员。


(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:

  1. Access a member or function of the class.
  2. Use pointer arithmetic.
  3. Use sizeof.
  4. Any RTTI information.
  5. new/delete, copy etc.
  6. Use it by value.
  7. Inherit from it.
  8. Have it as a member.
  9. 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屋!

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