# Virtual Functions and Inline Virtual Functions

> A virtual function in C++ is a base class method marked virtual so calls resolve at runtime through a vtable, using the object's real type rather than the pointer's declared type. A function can be both inline and virtual, but inlining is usually skipped for calls through a base pointer or reference, since inlining happens at compile time.

Canonical: https://devsunite.com/resources/core-subjects/oop/virtual-functions-and-inline-virtual-functions
Last updated: 2026-11-22

Subject: OOP · Category: Polymorphism · 4 min read

In C++, a function must be explicitly marked `virtual` in the base class for runtime polymorphism to actually happen. Without it, which function gets called is decided at compile time based on the reference or pointer's declared type, not the object's real type, even if a derived class defines a method with the same name.

## How it works

When a base class method is marked `virtual`, the compiler generates a virtual table (vtable) for the class, essentially a lookup table of function addresses. Each object of a class with virtual functions carries a hidden pointer to its class's vtable. Calling a virtual function through a base class pointer or reference looks up the actual function to run in the vtable at runtime, which resolves to whatever the real object's class actually defines.

## A worked example, with and without virtual

```cpp
#include <iostream>
using namespace std;

class Animal {
public:
    void speakNonVirtual() { cout << "Animal sound (non-virtual)" << endl; }
    virtual void speakVirtual() { cout << "Animal sound (virtual)" << endl; }
};

class Dog : public Animal {
public:
    void speakNonVirtual() { cout << "Woof! (non-virtual)" << endl; }
    void speakVirtual() override { cout << "Woof! (virtual)" << endl; }
};

int main() {
    Animal* a = new Dog();

    a->speakNonVirtual();  // prints "Animal sound (non-virtual)", resolved by the pointer TYPE
    a->speakVirtual();     // prints "Woof! (virtual)" , resolved by the REAL object's type

    delete a;
    return 0;
}
```

Both `Animal* a` variables point to an actual `Dog` object, but `speakNonVirtual()` calls the `Animal` version because it's resolved at compile time based purely on the pointer's declared type. `speakVirtual()` correctly calls the `Dog` version because it's resolved at runtime through the vtable, based on what `a` actually points to.

## Inline virtual functions

A function can technically be marked both `inline` and `virtual`, but in practice the `inline` request is usually ignored by the compiler whenever the call happens through a base class pointer or reference, because the whole point of `inline` is to let the compiler substitute the function's code directly at the call site at compile time, and a virtual call's whole purpose is to defer that decision to runtime, the two goals directly conflict. The compiler can still inline a virtual call when it can prove the object's exact type at compile time (for example, calling a virtual method directly on a concrete, non-pointer object), but that's an optimization detail, not something to rely on.

## Where this matters in real systems

Virtual functions are the actual mechanism behind interface-based design in C++, game engines define a base `GameObject` class with virtual `update()` and `render()` methods, and every specific object type (`Player`, `Enemy`, `Projectile`) overrides them, letting the engine's main loop call `update()` on a list of `GameObject*` pointers and get correct, type-specific behavior for every object without needing a giant switch statement checking types manually.

## What interviewers actually ask about virtual functions

- **"What happens if you don't mark a base class method as `virtual` and a derived class defines the same method?"** The derived method hides the base method rather than overriding it, and calling it through a base class pointer/reference resolves to the base version at compile time, not the derived version, a common source of subtle bugs.
- **"How does a vtable work?"** Each class with virtual functions gets a table of function pointers; each object of that class carries a hidden pointer to its class's vtable, and calling a virtual function looks up the correct function address in that table at runtime.
- **"Is there a performance cost to virtual functions?"** Yes, a small one: an extra pointer indirection through the vtable at call time, and the compiler generally can't inline the call. It's usually negligible compared to the flexibility gained, but it's a real, measurable cost.
- **"What is a pure virtual function?"** A virtual function with no implementation in the base class (`virtual void draw() = 0;`), forcing every concrete derived class to provide its own implementation, and making the base class abstract, unable to be instantiated directly.

## Key takeaways

- A function must be marked `virtual` in C++ for calls through a base pointer/reference to resolve to the derived class's overridden version at runtime.
- Without `virtual`, a derived class's same-named method hides rather than overrides the base version.
- Virtual dispatch works through a vtable, a per-class lookup table of function addresses.
- `inline` and `virtual` generally conflict, since inlining is a compile-time decision and virtual dispatch is deliberately a runtime one.

## Related

- [Previous: Method Overriding](https://devsunite.com/resources/core-subjects/oop/method-overriding)
- [Next: Abstract Classes and Pure Virtual Functions](https://devsunite.com/resources/core-subjects/oop/abstract-classes-and-pure-virtual-functions)
- [More in Polymorphism: Overview of Polymorphism](https://devsunite.com/resources/core-subjects/oop/overview-of-polymorphism)
- [More in Polymorphism: Function Overloading](https://devsunite.com/resources/core-subjects/oop/function-overloading)
- [All 72 OOP interview questions](https://devsunite.com/resources/core-subjects/oop)
