Reputation: 7542
Is it appropriate to give the Definition of a small Function -- which is used by two projects and has up to 10 lines of code -- into a Headerfile?
It is because i can put the Headerfile into the include-Directory, which is shared by both Projects. Otherwise i have to maintain the same cpp-Files in each Project Source Directory.
What is the common way for this situation?
Thanks for your answers.
Upvotes: 0
Views: 232
Reputation: 11648
Why can't you define the same function individually in each of the projects? or What's the wrong in including the cpp files also?
Maybe you say, "It may change in the mean time and I will need the updated".
But let's say you add more functions in the header files and after sometime you will end up with your entire implementations in the header file.
Either you can include source file also or else define the 10 line separately in the another project too.
If you want (request) your function to be an inline
one, declare it in the header files. Or else if you use templates
all your definitions will be in the header files.
But in your case, I prefer to include cpp also or else define the function separately..
Upvotes: 0
Reputation: 73443
This is a non-C++ solution. If the source control you are using has the support for soft-links, then you can create the source file in one project directory and create softlinks in the other projects. So whatever code you modify in the original source file is automatically reflected on the other projects.
Upvotes: 0
Reputation:
You can put it in a header, but you need to define it as inline:
inline void f() {
// stuff
}
This will prevent multiple-definition errors if the header is included in two different translation units in the same project. Note that this does not mean that the compiler will necessarily inline the function code at the call site.
Alternatively, you can define it in a nameless namespace, which will make it local to the translation unit(s) you include it in:
namespace {
void f() {
// stuff
}
}
Upvotes: 2
Reputation: 16476
A general rule of mine is to put only getters/setters in headers.
Anything more complex than a few lines really should go in source file (.cpp).
Upvotes: 1