Hi,
Multiple variants of this question has been asked and answers have been given but I have a specific requirement that has been bothering me for quite a few days.
I am a hardware/fimrware engineer tinkering around with C++ so pardon my newbie questions. I have the following working:
I don't want executable code to be in header files because of obvious reasons like debugging nightmares, multiple instantiations (header includes) etc. Can you please help me figure our how to move code in derived1.h & derived2.h to their corresponding .cpp files?
NOTE: I am not OK with implementing this using dynamic polymorphism (pure virtual functions) because of CPU execution and RAM (virtual tables) expenses.
I don't want executable code to be in header files because of obvious reasons like debugging nightmares, multiple instantiations (header includes) etc. Can you please help me figure our how to move code in derived1.h & derived2.h to their corresponding .cpp files?
The issues you enumerate are not issues. Where code resides doesn't make it any harder to debug. Correct header files must guard against multiple inclusion regardless of where the function definitions are, and one needn't worry about multiple function instantiations for templated functions. Your compiler/linker will readily handle those.
On the other hand, you might consider getting rid of those global objects. ;)
Hi cire,
Thanks for replying. Do you mean to say it is OK to have executable code in header files? There will be a lot *_impl functions in each file and they will call HAL APIs of a microcontroller's peripherals (GPIO, I2C, SPI etc)
But would you be okay with #include "FOO.inl" or #include "BAZ.hidden_definitions" ?
The compiler must be able to see template definitions in the same translation unit as the declaration, so moving template definitions to a .cpp file would result in "undefined reference" errors. However, you can "hide" your code in a separate file that is merely an extension of the header file.
For your derived classes, it should be fine to move the function definitions to the appropriate .cpp files because there are no function/class templates (base has already been instantiated).
To be clear, no executable code is compiled when the compiler processes base.h. At the time that base.h is processed, the compiler doesn't know the type of the template argument <derived>. You can think of templates as a macro facility. The template (base) is not expanded until instantiated with an explicit type at lines 3-4 of main.cpp.
As Daleth said, derived1 and derived2 can be moved to respective .cpp files.
Please note that I or anybody else involved in this project will not be creating derivedX, we will just give an example and expect the endusers to create their own derivedX implementations.
I understand that base.h will not be expanded until the derived classes have been instantiated (main.cpp: 3, 4). I am more concerned about derivedX.h over which we have absolutely no control, we only give the template usage and it's left to the end user as to when and where they include these .cpp/.h files.
As Daleth said, derived1 and derived2 can be moved to respective .cpp files.
I do not know how to do this, can you please provide an example for this?