Balazs Lehel Balazs Lehel - 4 months ago 29
C++ Question

Is this good practice to include source files?

I'm working with functions.
Is it good practice, that I write the function in another .cpp file, and I include it in the main one?
Like this : #include "lehel.cpp".

Is this ok, or should I write the functions directly in the main.cpp file?


A good practice is to separate functionality into separate Software Units so they can be reused and so that a change to one unit has little effect on other units.

If lehel.cpp is included by main, this means that any changes in lehel.cpp will force a compilation of main. However, if lehel.cpp is compiled separately and linked in, then a change to lehel.cpp does not force main to be recompiled; only linked together.

IMHO, header files should contain information on how to use the functions. The source files should contain the implementations of the functions. The functions in a source file should be related by a theme. Also, keeping the size of the source files small will reduce the quantity of injected defects.