Skip to content
Related Articles

Related Articles

Improve Article

Can virtual functions be inlined?

  • Difficulty Level : Medium
  • Last Updated : 29 May, 2017

Virtual functions are used to achieve runtime polymorphism or say late binding or dynamic binding. Inline functions are used for efficiency. The whole idea behind the inline functions is that whenever inline function is called code of inline function gets inserted or substituted at the point of inline function call at compile time. Inline functions are very useful when small functions are frequently used and called in a program many times.

By default all the functions defined inside the class are implicitly or automatically considered as inline except virtual functions (Note that inline is a request to the compiler and its compilers choice to do inlining or not).

Whenever virtual function is called using base class reference or pointer it cannot be inlined (because call is resolved at runtime), but whenever called using the object (without reference or pointer) of that class, can be inlined because compiler knows the exact class of the object at compile time.

#include <iostream>
using namespace std;
class Base
    virtual void who()
        cout << "I am Base\n";
class Derived: public Base
    void who()
        cout << "I am Derived\n";
int main()
    // note here virtual function who() is called through
    // object of the class (it will be resolved at compile
    // time) so it can be inlined.
    Base b;
    // Here virtual function is called through pointer,
    // so it cannot be inlined
    Base *ptr = new Derived();
    return 0;

Effective C++, by Scott Meyers

This article is contributed by Meet Pravasi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :