Difference Between Virtual Function And Inline Function In C++

What Is A Virtual Function?

A virtual function is a member function within the base class that we redefine in a derived class. It is declared using the virtual keyword. When a class containing virtual function is inherited, the derived class redefines the virtual function to suit its own needs.  Virtual function ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for function call. Functions are declared with a virtual keyword in base class. They are mainly used to achieve runtime polymorphism.

What You Need To Know About Virtual Function

  • Virtual function must be declared in public section of class.
  • Virtual function cannot be static or friend function also cannot be the virtual function of another class.  
  • Virtual function is defined in base class and overridden in derived class. It is not mandatory for derived class to override, in that case base class version of function is used.
  • Virtual functions decrease the efficiency of code.
  • Virtual function is mainly used to achieve runtime polymorphism.
  • Virtual function may consist of virtual destructor but it cannot have a virtual constructor.
  • Virtual may use for dynamic linkage.

What Is An Inline Function?

Inline function is a normal function which is defined by the keyword inline, it is a short function which is expanded by the compiler and its arguments are evaluated only once. When an inline function is called, the compiler replaces all the calling statements with the function definition at run-time. Every time an inline function is called, the compiler generates a copy of the function’s code, in place, to avoid the function call. Inline functions save the overhead of pushing and popping variables on the stack during function calls.

What You Need To Know About Inline Function

  • Inline function is a normal function which is defined by the keyword inline.
  • Inline function can also be non-static.
  • Inline functions are the short length functions that are automatically made the inline functions without using the inline keyword inside the class.
  • Inline functions are used to increase the efficiency of code.
  • Inline function is used to achieve compile time polymorphism.
  • Inline function can also consist of inline constructor.
  • Inline function is used to reduce the function call overhead. 

Difference Between Virtual Function And Inline Function In Tabular Form

BASIS OF COMPARISON   VIRTUAL FUNCTION   INLINE FUNCTION
Description Virtual function must be declared in public section of class.   Inline function is a normal function which is defined by the keyword inline.  
Nature Virtual function cannot be static or friend function also cannot be the virtual function of another class.    Inline function can also be non-static.  
Definition Virtual function is defined in base class and overridden in derived class. It is not mandatory for derived class to override, in that case base class version of function is used.   Inline functions are the short length functions that are automatically made the inline functions without using the inline keyword inside the class.  
Effect Virtual functions decrease the efficiency of code.   Inline functions are used to increase the efficiency of code.  
Use Virtual function is mainly used to achieve runtime polymorphism.   Inline function is used to achieve compile time polymorphism.  
Destructor & Constructor Virtual function may consist of virtual destructor but it cannot have a virtual constructor.   Inline function can also consist of inline constructor.  
Function Virtual may use for dynamic linkage.   Inline function is used to reduce the function call overhead.