Skip to content

Method Resolution Order (MRO)

MRO defines the order in which C++ searches for a method when it's called on an object — especially important in inheritance hierarchies.

When you call a method, C++ needs to know — which class's version to pick?

Simple Single Inheritance

cpp
class A { public: void show() { cout << "A" << endl; } };
class B : public A { public: void show() { cout << "B" << endl; } };

int main() {
    B obj;
    obj.show(); // B::show() — child takes priority
}

Rule: Child class method always takes priority over parent.

Search Order — Without Virtual

cpp
class C : public B { /* no show() */ };

C obj;
obj.show();
// C → B → A (searched bottom up)
// Looks in C first → not found
// Looks in B → found! prints "B"

MRO with Virtual Functions (Runtime)

cpp
class A { public: virtual void show() { cout << "A" << endl; } };
class B : public A { public: void show() override { cout << "B" << endl; } };
class C : public B { public: void show() override { cout << "C" << endl; } };

A* ptr;
B b; ptr = &b; ptr->show(); // B — resolved at runtime via VTable
C c; ptr = &c; ptr->show(); // C — resolved at runtime via VTable

MRO in Multiple Inheritance

cpp
class D : public B, public C {};

D obj;
obj.show();       // ERROR — ambiguous! B or C?
obj.B::show();    // OK — explicitly pick B
obj.C::show();    // OK — explicitly pick C
// Search Order for D: D → B → C → A

MRO with Virtual Inheritance

cpp
class D : public B, public C {
public:
    void show() { cout << "D" << endl; }
};
// Search Order: D → B → C → A
// D's version found first — no ambiguity

Even with virtual inheritance, if D doesn't override — B and C are still ambiguous. D must override to resolve it.

VTable — How Runtime MRO Works

Animal VTable → Animal::sound()
Dog VTable    → Dog::sound()
Cat VTable    → Cat::sound()

At runtime:
A* ptr = new B();
ptr->show()
  → looks up B's VTable
  → finds B::show()
  → calls B::show()

MRO Resolution Rules Summary

  • Start from the most derived class
  • Search left to right in inheritance list
  • Go up the hierarchy if not found
  • Virtual functions → resolved at runtime via VTable
  • Ambiguity → use scope resolution explicitly

C++ vs Python MRO

C++Python
AlgorithmLeft to right, bottom upC3 Linearization
KeywordNo special keywordsuper() follows MRO
Check MRONot directly availableClassName.__mro__
AmbiguityCompiler errorResolved automatically

One-liner: "MRO defines the order C++ searches for a method — child before parent, left to right in multiple inheritance, with virtual functions resolved at runtime via VTable."