Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Overriding a pure virtual function with inline implementation

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading