Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 947789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:08:12+00:00 2026-05-15T23:08:12+00:00

Consider the following class hierarchy: base class Object with a virtual method foo() an

  • 0

Consider the following class hierarchy:

  • base class Object with a virtual method foo()
  • an arbitrary hierarchy with multiple inheritance (virtual and non-virtual); each class is a subtype of Object; some of them override foo(), some don’t
  • a class X from this hierarchy, not overriding foo()

How to determine which method will be executed upon a call of foo() on an object of class X in C++?

(I’m looking for the algorithm, not any specific case.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:08:13+00:00Added an answer on May 15, 2026 at 11:08 pm

    There is no MRO in C++ like Python. If a method is ambiguous, it is a compile-time error. Whether a method is virtual or not doesn’t affect it, but virtual inheritance will.


    The algorithm is described in the C++ standard §[class.member.lookup] (10.2). Basically it will find the closest unambiguous implementation in the superclass graph. The algorithm works like this:

    1. Suppose you want to look up a function f in class C.

    2. We define a look-up set S(f, C) being a pair of sets (Δ, Σ) representing all possibilities. (§10.2/3)

      • The set Δ is called the declaration set, which is basically all the possible f‘s.

      • The set Σ is called the subobject set, which contain the classes that these f‘s are found.

    3. Let S(f, C) include all f directly defined (or using-ed) in C, if any (§10.2/4):

      Δ = {f in C};
      if (Δ != empty)
        Σ = {C};
      else
        Σ = empty;
      S(f, C) = (Δ, Σ);
      
    4. If S(f, C) is empty (§10.2/5),

      • Compute S(f, Bi) where Bi is a base class of C, for all i.

      • Merge each S(f, Bi) into S(f, C) one by one.

        if (S(f, C) == (empty, empty)) {
          B = base classes of C;
          for (Bi in B)
            S(f, C) = S(f, C) .Merge. S(f, Bi);
        }
        
    5. Finally the declaration set is returned as the result of name resolution (§10.2/7).

      return S(f, C).Δ;
      
    6. The merge between two look-up sets (Δ1, Σ1) and (Δ2, Σ2) is defined as (§10.2/6):

      • If every class in Σ1 is a base class of at least one class in Σ2, return (Δ2, Σ2).
        (Similar for the reverse.)
      • Else if Δ1 ≠ Δ2, return (ambiguous, Σ1 ∪ Σ2).
      • Otherwise, return (Δ1, Σ1 ∪ Σ2)

        function Merge ( (Δ1, Σ1), (Δ2, Σ2) ) {
        
           function IsBaseOf(Σp, Σq) {
             for (B1 in Σp) {
               if (not any(B1 is base of C for (C in Σq)))
                 return false;
             }
             return true;
           }
        
           if      (Σ1 .IsBaseOf. Σ2) return (Δ2, Σ2);
           else if (Σ2 .IsBaseOf. Σ1) return (Δ1, Σ1);
           else {
              Σ = Σ1 union Σ2;
              if (Δ1 != Δ2)
                Δ = ambiguous; 
              else
                Δ = Δ1;
              return (Δ, Σ);
           }
        }
        

    For example (§10.2/10),

    struct V { int f(); };
    struct W { int g(); };
    struct B : W, virtual V { int f(); int g(); };
    struct C : W, virtual V { };
    
    struct D : B, C {
       void glorp () {
         f();
         g();
       }
    };
    

    We compute that

    S(f, D) = S(f, B from D) .Merge. S(f, C from D)
            = ({B::f}, {B from D}) .Merge. S(f, W from C from D) .Merge. S(f, V)
            = ({B::f}, {B from D}) .Merge. empty .Merge. ({V::f}, {V})
            = ({B::f}, {B from D})   // fine, V is a base class of B.
    

    and

    S(g, D) = S(g, B from D) .Merge. S(g, C from D)
            = ({B::g}, {B from D}) .Merge. S(g, W from C from D) .Merge. S(g, V)
            = ({B::g}, {B from D}) .Merge. ({W::g}, {W from C from D}) .Merge. empty
            = (ambiguous, {B from D, W from C from D})  // the W from C is unrelated to B.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 478k
  • Answers 478k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If it's acceptable to generate the SVG with Javascript you… May 16, 2026 at 5:30 am
  • Editorial Team
    Editorial Team added an answer why don't you simply use \1 instead of \2? preg_match_all("/\{(something(:else)?)\}(.*?)\{\/\\1\}/is",… May 16, 2026 at 5:30 am
  • Editorial Team
    Editorial Team added an answer You can't, and for a good reason. Say MyClass1 and… May 16, 2026 at 5:30 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.