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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:41:15+00:00 2026-05-30T19:41:15+00:00

I’m wondering whether or class data members from multiple levels of inheritance are allocated

  • 0

I’m wondering whether or class data members from multiple levels of inheritance are allocated contiguously in C++ compilers. For example if I have the following classes:

class Base
{
 public:
   int a;
};

class child_1 : public Base
{
 public:
   int b;
};

class child_2 : public child_1
{
 public:
   int c;
}

Will the following operation run and output what is expected on all compilers?

child_2 my_obj;

my_obj.a = 3; my_obj.b = 2; my_obj.c = 1;

Base* base_ptr = (Base*)&my_obj;
int* int_ptr = (int*)base_ptr;

cout << "Output 3: " << *int_ptr << endl
     << "Output 2: " << *(int_ptr + 1) << endl;
     << "Output 1: " << *(int_ptr + 2) << endl;

Furthermore, if this is possible, do I need an intermediate cast from the child_2* type to the Base* type as I have above or would the following also be permissable?

int* int_ptr (int*)&my_obj;

Please note that I’m not asking if this is a good idea. Obviously you would want to design your software so that you don’t end up doing stuff like this, but I’m considering such an approach to solve a problem with working with some older code (made by an experienced software engineer, which makes it more complicated).

The compiler that I’m specifically working with is VC++ 2005.

  • 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-30T19:41:16+00:00Added an answer on May 30, 2026 at 7:41 pm

    No, the relative layout of derived-class members and base-class members is not specified.

    In the current standard, a class is only standard-layout if, among other conditions, it:

    either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes with non-static data members

    In older standards (which would apply to your compiler), there wasn’t even a concept of standard-layout, only POD, and any class with a non-empty base class was not POD.

    Your code depends on how a particular compiler lays out the base classes, and is not portable.

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

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into

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.