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 8812831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:41:40+00:00 2026-06-14T03:41:40+00:00

Consider this C++ code: #include <iostream> using namespace std; struct B { virtual int

  • 0

Consider this C++ code:

#include <iostream>
using namespace std;

struct B {
    virtual int f() { return 1; }
    int g() { return 2; }
};
struct D1 : public B { // (*)
    int g() { return 3; }
};
struct D2 : public B { // (*)
    virtual int f() { return 4; }
};
struct M : public D1, public D2 {
    int g() { return 5; }
};

int main() {
    M m;
    D1* d1 = &m;
    cout << d1->f()
         << static_cast<D2&>(m).g()
         << static_cast<B*>(d1)->g()
         << m.g();
}

It prints 1225. If we make virtual inheritance, i.e. add virtual before public in lines marked with (*), it prints 4225.

  1. Can you explain why 1 changes to 4?
  2. Can you explain meaning of static_cast<D2&>(m) and static_cast<B*>(d1)?
  3. How you are you not getting lost in this kind of combinations? Are you drawing something?
  4. Is it common to spot such complex settings in normal projects?
  • 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-06-14T03:41:41+00:00Added an answer on June 14, 2026 at 3:41 am

    (1) Can you explain why 1 changes to 4?

    Without virtual inheritance, there are 2 independent hierarchies of inheritance; B->D1->M and B->D2->M. So imagine 2 virtual function tables (though this is implementation defined).
    When you invoke f() with D1*, it just knows about B::f() and that’s it. With virtual inheritance, base class B is delegated to M and thus D2::f() is considered as part of class M.

    (2) Can you explain meaning of static_cast<D2&>(m) and static_cast<B*>(d1)?

    static_cast<D2&>(m), is like considering object of class M as class D2
    static_cast<B*>(d1), is like considering pointer of class D1 as class B1.
    Both are valid casts.
    Since g() is not virtual the function choice happens at compile-time. Had it been virtual then all these casting won’t matter.

    (3) How you are you not getting lost in this kind of combinations? Are you drawing something?

    Ofcourse it’s complex and at first glance if there are so many of such classes, one might get easily lost.

    (4) Is it common to spot such complex settings in normal projects?

    Not at all, it’s unusual and sometimes a code smell.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code: #include <iostream> using namespace std; int main() { bool lock =
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
Consider this code: #include <iostream> using namespace std; typedef int array[12]; array sample; array
Consider this code #include <iostream> #include <cstdio> using namespace std; class Dummy { public:
Consider the following sample code: #include <iostream> using namespace std; class base { public:
Consider the following code: #include<iostream> #include<vector> using namespace std; class Foo { public: template<
Consider this code: #include <iostream> template<class C> struct time { }; int main() {
Consider this piece of code: #include <vector> #include <iostream> using namespace std; class Base
Consider the following code: #include <iostream> using namespace std; class Test { static int
Consider this code: #include <iostream> void f(int&& i) { std::cout << f(int&&)\n; } void

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.