Header files inclusion / Forward declaration

后端 未结 5 904
北海茫月
北海茫月 2020-12-24 03:39

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

5条回答
  •  不思量自难忘°
    2020-12-24 04:02

    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.

提交回复
热议问题