The Wayback Machine - https://web.archive.org/web/20240914031008/https://www.geeksforgeeks.org/inline-virtual-function/
Open In App

Can Virtual Functions be Inlined in C++?

Last Updated : 01 Dec, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Virtual functions are member functions that are declared in the base class using the keyword virtual and can be overridden by the derived class. They are used to achieve Runtime polymorphism or say late binding or dynamic binding. 

Inline functions are used to replace the function calling location with the definition of the inline function at compile time. They are used for efficiency. The whole idea behind the inline functions is that whenever an inline function is called code of the inline function gets inserted or substituted at the point of the 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: inline is a request to the compiler and its compilers choice to do inlining or not.

Can virtual functions be inlined?

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

C++




// CPP program to demonstrate that
// virtual functions can be inlined
#include <iostream>
using namespace std;
 
class Base {
public:
    virtual void who() { cout << "I am Base\n"; }
};
class Derived : public Base {
public:
    void who() { cout << "I am Derived\n"; }
};
 
int main()
{
    // Part 1
    Base b;
    b.who();
 
    // Part 2
    Base* ptr = new Derived();
    ptr->who();
 
    return 0;
}


Output

I am Base
I am Derived

Explanation: In Part 1, the virtual function who() is called through the object of the class. Since it will be resolved at compile-time, so it can be inlined. In Part 2, the virtual function is called through a pointer, so it cannot be inlined.


Previous Article
Next Article

Similar Reads

Can Static Functions Be Virtual in C++?
In C++, a static member function of a class cannot be virtual. Virtual functions are invoked when you have a pointer or reference to an instance of a class. Static functions aren't tied to the instance of a class but they are tied to the class. C++ doesn't have pointers-to-class, so there is no scenario in which you could invoke a static function v
1 min read
Can Virtual Functions be Private in C++?
A virtual function can be private as C++ has access control, but not visibility control. As mentioned virtual functions can be overridden by the derived class but under all circumstances will only be called within the base class. Example 1: Calling Private Virtual Function from Base Class Pointer to Derived Class Instance [GFGTABS] C++ // C++ progr
4 min read
What happens when a virtual function is called inside a non-virtual function in C++
Predict the output of the following simple C++ program without any virtual function. C/C++ Code #include &amp;lt;iostream&amp;gt; using namespace std; class Base { public: void print() { cout &amp;lt;&amp;lt; &amp;quot;Base class print function \n&amp;quot;; } void invoke() { cout &amp;lt;&amp;lt; &amp;quot;Base class invoke function \n&amp;quot;;
2 min read
Difference between Virtual function and Pure virtual function in C++
Virtual Function in C++ A virtual function is a member function which is declared within a base class and is re-defined(Overridden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function. Pu
2 min read
Virtual Functions in Derived Classes in C++
A virtual function is a member function of a base class that is overridden by a derived class. When you use a pointer or a reference to the base class to refer to a derived class object, you can call a virtual function for that object and have it run the derived class's version of the function. In C++, once a member function is declared as a virtua
2 min read
Virtual Functions and Runtime Polymorphism in C++
A virtual function is a member function that is declared in the base class using the keyword virtual and is re-defined (Overridden) in the derived class. It tells the compiler to perform late binding where the compiler matches the object with the right called function and executes it during the runtime. This technique falls under Runtime Polymorphi
8 min read
C++ | Virtual Functions | Question 1
Which of the following is true about virtual functions in C++. (A) Virtual functions are functions that can be overridden in derived class with the same signature. (B) Virtual functions enable run-time polymorphism in a inheritance hierarchy. (C) If a function is 'virtual' in the base class, the most-derived class's implementation of the function i
1 min read
C++ | Virtual Functions | Question 2
Predict output of the following program #include&lt;iostream&gt; using namespace std; class Base { public: virtual void show() { cout&lt;&lt;&quot; In Base \n&quot;; } }; class Derived: public Base { public: void show() { cout&lt;&lt;&quot;In Derived \n&quot;; } }; int main(void) { Base *bp = new Derived; bp-&gt;show(); Base &amp;br = *bp; br.show(
1 min read
C++ | Virtual Functions | Question 3
Output of following program #include&lt;iostream&gt; using namespace std; class Base { public: virtual void show() { cout&lt;&lt;&quot; In Base \n&quot;; } }; class Derived: public Base { public: void show() { cout&lt;&lt;&quot;In Derived \n&quot;; } }; int main(void) { Base *bp, b; Derived d; bp = &amp;d; bp-&gt;show(); bp = &amp;b; bp-&gt;show();
1 min read
C++ | Virtual Functions | Question 5
#include&lt;iostream&gt; using namespace std; class Base { public: virtual void show() = 0; }; int main(void) { Base b; Base *bp; return 0; } (A) There are compiler errors in lines "Base b;" and "Base bp;" (B) There is compiler error in line "Base b;" (C) There is compiler error in line "Base bp;" (D) No compiler Error Answer: (B) Explanation: Sinc
1 min read
C++ | Virtual Functions | Question 6
Predict the output of following program. #include&lt;iostream&gt; using namespace std; class Base { public: virtual void show() = 0; }; class Derived : public Base { }; int main(void) { Derived q; return 0; } (A) Compiler Error: there cannot be an empty derived class (B) Compiler Error: Derived is abstract (C) No compiler Error Answer: (B) Explanat
1 min read
C++ | Virtual Functions | Question 7
#include&lt;iostream&gt; using namespace std; class Base { public: virtual void show() = 0; }; class Derived: public Base { public: void show() { cout&lt;&lt;&quot;In Derived \n&quot;; } }; int main(void) { Derived d; Base &amp;br = d; br.show(); return 0; } (A) Compiler Error in line "Base &amp;br = d;" (B) Empty Output (C) In Derived Answer: (C)
1 min read
C++ | Virtual Functions | Question 14
Can a constructor be virtual? Will the following program compile? #include &lt;iostream&gt; using namespace std; class Base { public: virtual Base() {} }; int main() { return 0; } (A) Yes (B) No Answer: (B) Explanation: There is nothing like Virtual Constructor. Making constructors virtual doesn't make sense as constructor is responsible for creati
1 min read
C++ | Virtual Functions | Question 9
Can a destructor be virtual? Will the following program compile? #include &lt;iostream&gt; using namespace std; class Base { public: virtual ~Base() {} }; int main() { return 0; } (A) Yes (B) No Answer: (A) Explanation: A destructor can be virtual. We may want to call appropriate destructor when a base class pointer points to a derived class object
1 min read
C++ | Virtual Functions | Question 10
#include&lt;iostream&gt; using namespace std; class Base { public: Base() { cout&lt;&lt;&quot;Constructor: Base&quot;&lt;&lt;endl; } virtual ~Base() { cout&lt;&lt;&quot;Destructor : Base&quot;&lt;&lt;endl; } }; class Derived: public Base { public: Derived() { cout&lt;&lt;&quot;Constructor: Derived&quot;&lt;&lt;endl; } ~Derived() { cout&lt;&lt;
1 min read
C++ | Virtual Functions | Question 11
Can static functions be virtual? Will the following program compile? #include&lt;iostream&gt;  using namespace std;      class Test {    public:       virtual static void fun()  { } }; (A) Yes (B) No Answer: (B) Explanation: Static functions are class specific and may not be called on objects. Virtual functions are called according to the pointed o
1 min read
C++ | Virtual Functions | Question 12
Predict the output of following C++ program. Assume that there is no alignment and a typical implementation of virtual functions is done by the compiler. #include &lt;iostream&gt; using namespace std; class A { public: virtual void fun(); }; class B { public: void fun(); }; int main() { int a = sizeof(A), b = sizeof(B); if (a == b) cout &lt;&lt;
1 min read
C++ | Virtual Functions | Question 13
#include &lt;iostream&gt; using namespace std; class A { public: virtual void fun() { cout &lt;&lt; &quot;A::fun() &quot;; } }; class B: public A { public: void fun() { cout &lt;&lt; &quot;B::fun() &quot;; } }; class C: public B { public: void fun() { cout &lt;&lt; &quot;C::fun() &quot;; } }; int main() { B *bp = new C; bp-&gt;fun(); return 0; } (A
1 min read
C++ | Virtual Functions | Question 14
Predict the output of following C++ program #include&lt;iostream&gt; using namespace std; class Base { public: virtual void show() { cout&lt;&lt;&quot; In Base \n&quot;; } }; class Derived: public Base { public: void show() { cout&lt;&lt;&quot;In Derived \n&quot;; } }; int main(void) { Base *bp = new Derived; bp-&gt;Base::show(); // Note the use of
1 min read
Pure Virtual Functions and Abstract Classes in C++
Sometimes implementation of all functions cannot be provided in a base class because we don't know the implementation. Such a class is called an abstract class.For example, let Shape be a base class. We cannot provide the implementation of function draw() in Shape, but we know every derived class must have an implementation of draw(). Similarly, an
6 min read
Default Arguments and Virtual Function in C++
Default Arguments are the values provided during function declaration, such that values can be automatically assigned if no argument is passed to them. In case any value is passed the default value is overridden and it becomes a parameterized argument. Virtual function is a member function that is declared within a base class and is redefined(Overr
3 min read
Virtual Function in C++
A virtual function (also known as virtual methods) is a member function that is declared within a base class and is re-defined (overridden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the meth
6 min read
Difference between virtual function and inline function in C++
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 th
2 min read
Target Corporation Interview Experience | On-Campus Virtual Hiring
Round 1 - Online test This round has MCQ s regrading English reading comprehension, Reasoning, logical, Quantitative(Numerical) and technical questions. Technical questions were related to computer science domain. This round also has 2 coding questions: Counting the duplicate elements in an arrayOptimal oil flow problem Both questions had 2 public
3 min read
ARM Interview Experience for Intern (On-Campus Virtual 2020)
ARM talent acquisition team visited our campus virtually for an intern role in the verification domain. There were totally 3 rounds, Round 1 (Written Test) : 30 Aptitude (Time &amp; Work, Time and Distance, percentages, profit and loss, SI &amp; CI covered all major topics in aptitude)70 Technical (Digital, C basics &amp; Guessing the output, OS co
2 min read
Difference Between Friend Function and Virtual Function in C++
A friend class can access private and protected members of other classes in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other classes. Just likely, a friend function is a function that is declared outside the scope of a class. This function can be invoked like a normal function and
3 min read
Virtual Destructor
Deleting a derived class object using a pointer of base class type that has a non-virtual destructor results in undefined behavior. To correct this situation, the base class should be defined with a virtual destructor. For example, the following program results in undefined behavior. C/C++ Code // CPP program without virtual destructor // causing u
2 min read
How Does Default Virtual Behavior Differ in C++ and Java?
Let us discuss how the default virtual behavior of methods is opposite in C++ and Java. It is very important to remember that in the C++ language class member methods are non-virtual by default. They can be made virtual by using virtual keywords. For example, Base::show() is non-virtual in following program and program prints "Base::show() called".
3 min read
Advanced C++ | Virtual Copy Constructor
In the virtual constructor idiom we have seen the way to construct an object whose type is not determined until runtime. Is it possible to create an object without knowing it's exact class type? The virtual copy constructor address this question.Sometimes we may need to construct an object from another existing object. Precisely the copy constructo
5 min read
Pure Virtual Destructor in C++
A pure virtual destructor can be declared in C++. After a destructor has been created as a pure virtual object(instance of a class), where the destructor body is provided. This is due to the fact that destructors will not be overridden in derived classes, but will instead be called in reverse order. As a result, for a pure virtual destructor, you m
4 min read
Practice Tags :