Open In App

Difference between virtual function and inline function in C++

Improve
Improve
Like Article
Like
Save
Share
Report

Virtual function: Virtual function is a member function which is declared within a base class and is redefined by a derived class.

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.

The syntax of defining the function inline in C++ is:

inline return-type function-name(parameters)
{
    // function code
} 

Difference between virtual function and inline function are as follows:

Virtual function

Inline function

1. Virtual function must be declared in public section of class. 1. Inline function is a normal function which is defined by the keyword inline.
2. Virtual function cannot be static. 2. Inline function can also be non-static.
3. Virtual function is defined in base class. 3. Inline function are the short length functions that are automatically made the inline functions without using the inline keyword inside the class.
4. Virtual function are used to decrease the efficiency of code. 4. Inline function are used to increase the efficiency of code.
5. Virtual function is to run time polymorphism. 5. Inline function is to compile time polymorphism.
6. Virtual function may consists of virtual destructor but it cannot have a virtual constructor.  6. Inline function can also consist of inline constructor.
7. Virtual may use for dynamic linkage. 7. Inline function is used to reduce the function call overhead.

Last Updated : 18 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads