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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:45:25+00:00 2026-06-13T14:45:25+00:00

The following code are from the book Inside the C++ object model #include <iostream>

  • 0

The following code are from the book “Inside the C++ object model”

#include <iostream>  
using namespace std;
class X{};
class Y: public virtual X{};
class Z: public virtual X{};
class A: public Y, public Z{};

int main()
{
     cout<<sizeof(X)<<" "<<sizeof(Y)<<" "<<sizeof(Z)<<" "<<sizeof(A)<<endl;
     return 0;
}

In my computer(Windows, VS2010), the output is:

1 4 4 8

Here’re my questions

1, sizeof(X)=1

The book says when X type generate two instance, say xa and xb. the compile insert a byte into A so that xa and xb can have different address. I’m not quite understand the reasons.

2, sizeof(Y)=4

By using virtual inheritance, will we have an additional virtual pointer? I guess this might be different from virtual pointer in polymorphism. Can anyone give me the memory layout for Y?

Thank you!

  • 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-13T14:45:26+00:00Added an answer on June 13, 2026 at 2:45 pm
    1. compiler new one char when the class is empty, so it can generate different object
    2. sizeof(Y)=4 because it’s virtual inheritance, construct will generate vptr table which is 4 bytes on 32-bit system
    3. if you are using visual studio use /d1reportAllClassLayout in properties->C/C++/Command to generate object layout
      class Y object layout will be on Visual Studio:
    4. book ‘Inside C++ object model’ by Stanley B. Lippman explained this extremely well

     
            class Y size(4):
                +---
                0     | {vbptr}
                +---
                +--- (virtual base X)
                +---
    Y::$vbtable@: 0 | 0 1 | 4 (Yd(Y+0)X)

    vbi: class offset o.vbptr o.vbte fVtorDisp
    X 4 0 4 0

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

Sidebar

Related Questions

From the book Groovy and Grails recipes I'm using the following code snippet: String
I run the following code from command line: public class MemoryTest { public static
The following code snippet is from book Effective C#, public event AddMessageEventHandler Log; public
I saw the following code, public override bool Equals(object obj) { // From the
I'm using some code from a book to implement a generic repository class for
I have the following piece of code from a book. There is this function
in the following piece of code I got from a book. The NSString *pPath
the following code (from a database object created for each new view): -(void)update:(NSString *)queryText{
Hi I found the following code from this page JQuery UI DatePicker using 2
Given the following code: public class A { static final long tooth = 1L;

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.