C ++ functions without header file
Do standalone C ++ functions need a header file and a code file?
I am writing some C ++ functions that use some of my other C ++ classes but do not belong to the class itself. They are meant to be compiled into dlls and so I was wondering if I need to declare them in a separate header file or if I could / should just put the declarations in a .cc file.
Could it be just bad practice?
source to share
The header file is useful because it allows other source files to know about the functionality declared in another translation unit.
This is necessary so that the compiler can check that what you are calling is correct for type checking. But the need comes from the declaration itself, not from the existence of the header file.
For DLLs, if I recall correctly, you are not forced to do this just because you will still declare the function signature when you use them, for example.
extern C __declspec(dllimport) void foo();
Of course, this means that you will need to redirect the ad anyway, so I don't see a problem with the header files for your DLL, it will just keep all the signatures together.
source to share
If you are going to use a function outside the source file in which it is defined, you absolutely need a declaration. Your best bet is to put your declaration in a header file to keep it consistent, otherwise you just end up repeating yourself and introducing a potential source of errors.
source to share
This is optional, but highly recommended.
Place the function declarations in the header file and the function definitions in the source (.cc) file. The motivation is to allow users (here, other programmers) to view only the interface, not the implementation (as it might change). Plus, it allows other source files to include your header file and use the functions you provide.
The only exceptions are static functions, they should not be declared in the header file as they should not be viewed or used outside the source file.
source to share