I would like a function that is not a member of a class and is accessible from any class.
I assume I would have to #include the header file where the function is declared, but I don’t know where to define such a global function.
Are there good reasons against having such a function in the first place?
you need a body (in a
cppfile):and a definition/prototype in a header file, which will be included before any use of the function:
then using it somewhere else:
or use an inline function (doesn’t guarantee it’ll be inlined, but useful for small functions, like
foo):alternatively you can abuse the C-style header based functions (so this goes in a header, the
staticforces it to exist in a single compilation unit only, you should avoid this however):