Re: inlining virtual functions
* Martin Bonner:
There are two things going on here called "inlining". One is adding
the "inline" keyword to a function definition (or implicitly declaring
a member function as inline by defining it in the class body). The
other is the compiler replacing a call to a function with the
instructions that constitute the body of the function.
Right.
[snip]
Adding the "inline" keyword to a function definition means that the
function has to be defined in a header file, rather than in a .cpp
file.
Sorry, that's incorrect. First, the C++ standard does not differentiate
header files and implementation files. Second, for a compiler where
'inline' actually has some effect on inlining, one might use that
property in an implementation file -- and that's not not uncommon.
This is bad because it increases coupling between translation
units.
No, having a function inline in a header file won't necessarily increase
coupling.
Given that the compiler probably won't inline your virtual functions,
you might as well put them in the .cpp file, rather than cluttering up
the header file with them.
Again no, the decision to inline (textually) or not has, as I wrote,
little or nothing to do with resulting machine code inlining, at least
if the programmer is knowledgable about what 'inline' means.
If you are putting them in the .cpp file,
you don't need (and mustn't have) the "inline" keyword.
Sorry, that's incorrect (see above).
... and all of this is obviously different if you are writing templates
where you have to put all the functions in the header file until export
is reliably portable.
Sorry, that's incorrect. Only in the case of possible instantiation on
an unbounded number of types (which is the most common common case) is
it necessary to make the template implementation available to the
compiler. As an example where that isn't necessary, consider a function
template< typename CharT >
std::basic_string<CharT> userName();
which can and most probably should be implemented in a separate
implementation file, specialized for 'char' and 'wchar_t'.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]