What should go into an .h file?

后端 未结 12 941
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 15:26

When dividing your code up into multiple files just what exactly should go into an .h file and what should go into a .cpp file?

12条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 15:42

    The header Defines something but doesn't tell anything about the implementation. ( Excluding Templates in this "metafore".

    With that said, you need to divide "definitions" into sub-groups, there are, in this case, two types of definitions.

    • You define the "layout" of your strucutre, telling only as much as is needed by the surrounding usage groups.
    • The definitions of a variable, function and a class.

    Now, I am of course talking about the first subgroup.

    The header is there to define the layout of your structure in order to help the rest of the software use the implementation. You might want to see it as an "abstraction" of your implementation, which is vaughly said but, I think it suits quite well in this case.

    As previous posters have said and shown you declare private and public usage areas and their headers, this also includes private and public variables. Now, I don't want to go into design of the code here but, you might want to consider what you put in your headers, since that is the Layer between the end user and the implementation.

提交回复
热议问题