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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:29:35+00:00 2026-05-30T01:29:35+00:00

I have the following structs struct A { int i; A() { i =

  • 0

I have the following structs

struct A 
{
   int i;
   A() { i = 0; }
   A(int _i) : i(_i) {}
   virtual void f() { cout << i; }
};



struct B1 : virtual A {
   B1() : A(1) { f(); }
   void f() { cout << i+10; }
};


struct B2 : virtual A {
   B2(int i) : A(2) { f(); }
};


struct C : B1, B2 {
   C() : B2(3) {}
};

please explain why the following code prints 100: (10+0)

 C* c = new C();

I think it should print 1111:
first A() : i = 0
then B1() : i = 1 and prints B1::f() – 11
then B2() : prints prints B1::f() – 11

Where I go wrong?

Thanks

  • 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-30T01:29:37+00:00Added an answer on May 30, 2026 at 1:29 am

    Due to the inheritance, the C object “contains” a B1 object and a B2 object. Both the B1 and the B2 object “contain” an A object, but since you have virtual inheritance of A, you only have one A object in each C object, not two. This single A object is initialized using A’s default constructor.

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

Sidebar

Related Questions

Suppose we have the following code: #include <iostream> struct A { virtual void f()
I have the following code: class outer { struct inner { int var1; int
I have the following code: #include <boost/shared_ptr.hpp> struct Foo { int a; }; static
I have the following code int ParseData(unsigned char *packet, int len) { struct ethhdr
The following code: struct interface_base { virtual void foo() = 0; }; struct interface
I have some trouble understanding the output from the following code: int main(void) {
I have the following C++ structs struct InnerStruct { int A; int B; };
Say I have a struct like the following: struct ParentStruct { virtual void XYZ()
I wrote the following code snippet: void foo() { struct _bar_ { int a;
Having the following code: #include <iostream> struct A { int x; A(){} ~A(){std::cout <<~A(<<x<<)\n;}

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.