简体   繁体   中英

Difference in including the .cpp file and .h file (with the same content in cpp)?

I've recently started learning cpp from basics and was very much confused with the folowing:

Lets say I have a header( test.h which contains only declarations) with some content and some source file ( source.cpp ) and program produced some result.

If I have copied the same content of that header file to a .cpp file ( testcpp.cpp ) and included this in source.cpp

In this case, I did not understood what difference it makes?

( I'll not include this testcpp.cpp in make file )

I have seen some threads similar to this but couldn't get a clear idea!!!

I learnt the usage of header and cpp files and have used it correctly in projects till now, Please answer specific to this scenario (I know doing this way adds confusion but just want to know). Will there be any difference doing so or it's just a common practice everyone follows ?

It changes nothing. It's just a convention whether you use a *.h or *.cpp or *.asdasd suffix, as long as it doesn't get compiled by itself.

Some projects use the .hxx extension for header files and .cc for source file.

Please, for the good of fellow programmers you'll work with, stick to common conventions and don't put header code in .cpp files.

what difference it makes?

The extension of a header file has no effect on anything. You could have just as well named the file test.mpg , .test or just test (changing the include directive obviously), and it would have worked just as well. The extension is for the benefit of the programmer, not the toolchain.

However, it is a bad idea to name it anything other than .h, .hpp or whatever is your convention. If you name it .mpg, people will think that it is a video, and not realising that it is a header file, try to play it in a media player. If you name it .cpp, people will think that it is a source file and may attempt to compile it or maybe add definitions into it.

Including a file with the preprocessor is technically just copying contents of one file into another. Nothing more and nothing less. Everything else about them is just convention.

In makefile, when specifying source file, Can I give my source files with any extension(.fsfs, .xxx) rather than .cpp extension

Technically yes, however compilers usually use the source file extension to detect the language which they will fail to do in this case, so you would have to specify it explicitly.

#include just does a copy-n-paste of the file you include into the current file. What the file is named doesn't matter one bit - you can name it "foo.exe" if you like; as long as it contains valid source-code in the context where it is included all is well (but please don't use unconventional names, you'll just confuse people).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM