What is the difference between a .cpp file and a .h file?

后端 未结 8 2086
余生分开走
余生分开走 2020-12-02 11:52

Because I\'ve made .cpp files then transfered them into .h files, the only difference I can find is that you can\'t #include .cpp files. Is there any difference that I am m

8条回答
  •  不知归路
    2020-12-02 12:31

    I know the difference between a declaration and a definition.

    Whereas:

    • A CPP file includes the definitions from any header which it includes (because CPP and header file together become a single 'translation unit')
    • A header file might be included by more than one CPP file
    • The linker typically won't like anything defined in more than one CPP file

    Therefore any definitions in a header file should be inline or static. Header files also contain declarations which are used by more than one CPP file.

    Definitions that are neither static nor inline are placed in CPP files. Also, any declarations that are only needed within one CPP file are often placed within that CPP file itself, nstead of in any (sharable) header file.

提交回复
热议问题