Member functions with their body inside the class definition are implicitly inline
, so your two code samples are the same.
However it is different if the body is provided out-of-line:
class foo
{
public:
int example1( int i );
inline int example2( int i );
};
In this code, foo::example2
is an inline function. The out-of-line definition must also appear in the header (unless the function is never odr-used). It's optional whether the out-of-line definition also includes the inline
specifier.
For foo::example1
, if there is an out-of-line definition with inline
specifier, then the function is inline
. Again, if such a definition is present then it must be in the header unless the function is never odr-used.
These ODR violations are undefined behaviour with no diagnostic required. In practice this is likely to mean it may "appear to work", or give a link error.
Typically, you would only use my example2
case where you want the function definition to appear in the header but you think it is more readable to have the class definition compact and the function body later on.
In this case I would recommend using inline
in both the declaration and definition, to enhance readability.
Note for Windows programming: If the class is marked __declspec(dllexport)
or __declspec(dllimport)
, then gcc warns about example1
. I'm not sure what the correct behaviour is here but to avoid any possible issues, use inline
in the declaration.