I've come across a piece of code which boils down to this:
class Base
{
virtual void foo() = 0;
};
class Derived : public Base
{
inline void foo() { /* Implementation */}
};
I know the person who wrote this code is coming from a C background, so it may not be a correct practice. I understand that Derived::foo
is implicitly virtual, but I'm a bit confused on whether the implementation of a pure virtual function can be inlined at all. Is this considered an ok practice? Does this inline function actually get placed in the vtable (although I imagine it would result in a compiler error otherwise)? Also, is inline keyword completely redundant, as definition in the class should imply inlining in the first place?
CodePudding user response:
The method is already implicitly inline
because it appears in the class definition.
inline
is not what you think it is. It is not to control whether calls to the function are inlined by the compiler. The compiler will decide this independent of the attribute inline
. It merely says: The definition can be in a header, no problem with multiple definitions will arise.
For example you could place all this in a header:
class Derived : public Base
{
void foo();
};
inline Derived::foo() { /* implementation */ }
When the member function definition is in the class body then it is implicitiy inline
.