Sharon wrote:
Usually it is common to write the class member function in the class
H file, but some people like to write the function body in the C++
file.
Can anybody tell me what are the cases where inline function should
not be written in the C++ file? Or what are the disadvantages of
inline function body in a C++ file?
I'll assume that by "C++ file" you mean ".CPP" file. The .H file is just as
much C++.
Inline function bodies have to be visible to the compiler in every
translation unit where they're used. The most common way to ensure that is
to simply put the inline definitions in the same header file where they're
declared.
If a function has been declared as inline but the definition is not visible
to the compiler, the compiler simply cannot continue: since the function is
inline, the compiler can't generate a call to an out of line funciton,
neither can it make something up (of course).
If a function is used in only one .CPP file, that function's definition can
of course be placed in that .CPP file. The disadvantage to doing that, of
course, is that if you subsequently want to use that function in another
..CPP file, you'll have to move the definition. The wrong solution is to put
duplicate definitions of the function into two files. That's just asking
for trouble down the line when someone modifies one copy of the function and
not another. It's also a violation of what's known as the "One Definition
Rule".
-cd