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

  • Home
  • SEARCH
  • 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 586701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:08:04+00:00 2026-05-13T15:08:04+00:00

Possible Duplicate: C++ pointer multi-inheritance fun. (Follow up on: C++ pointer multi-inheritance fun )

  • 0

Possible Duplicate:
C++ pointer multi-inheritance fun.

(Follow up on: C++ pointer multi-inheritance fun )

I’m writing some code involving inheritance from a basic ref-counting pointer class; and some intricacies of C++ popped up. I’ve reduced it as follows:

Suppose I have:

class A{int x, y;};
class B{int xx, yy;};
class C: public A, public B {int z;};

C c;
C* pc = &c;
B* pb = &c;
A* pa = &c;

// does pa point to a valid A object?
// does pb point to a valid B object?

// does pa == pb ?

Furthermore, does:

// pc == (C*) pa ?
// pc == (C*) pb ?

Thanks!

Here, “==” means points at the same value.

  • 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-13T15:08:04+00:00Added an answer on May 13, 2026 at 3:08 pm
    • Yes
    • Yes
    • No
    • Yes
    • No*

    But you need to know how C++ organises memory. The layout of a class, CClass, is as follows:

    offset
    0                              First base class in list of base classes
    sizeof first base class        Next base class
    sizeof first N-1 base classes  Nth base class
    sizeof all base classes        CClass
    

    OK, it’s a bit more complex than that if there is an inheritance tree, but you should get the basic idea. Assuming an int is four bytes then class C is laid out like:

    offset
    0             A::x
    4             A::y
    8             B::xx
    12            B::yy
    16            C:z
    

    But the an object of type C is all of the above, so a pointer to a C is pointing to offset 0. However, C++ allows implicit downcasting, so a pointer to a C can be converted to a pointer to an A or a pointer to a B. But if you look at the above, a pointer to a B is at offset 8 rather than 0 (a pointer to a C) and that a pointer to an A is at offset 0. So, casting a pointer to a C to a pointer to a B adds 8 to the pointer value. This is because the methods of B assume the ‘this’ pointer points to B’s first member (B::xx), but a pointer to a C if reinterpreted as a pointer to a B (i.e. the value is the same) would be pointer to an address eight bytes before where B actually is, so that all of B’s methods would be using, in this instance, A’s members.

    Upcasting (the final two conversions) is different kettle of fish. Going from a pointer to a B to a pointer to a C is really hard because you don’t know if the pointer to a B is just pointing to an instance of B or at an instance of C plus eight. This is where RTTI and the dynamic cast comes in. With RTTI (Run Time Type Information) enabled, the pointer to B contains additional information that describes what B really is – a simple B or a B as part of a C. This does have additional cost, both execution time and memory usage.

    Finally, this does highlight the ambiguity of the C style cast. You really should use the C++ style casts (static_cast <>, etc) as this clarifies the way the conversion should be done.

    *This could also be a yes, I guess it depends on the compiler and if RTTI is on or off. You would need to get into the details of the standard and the compiler implementation to say for sure.

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

Sidebar

Related Questions

Possible Duplicates: C++ pointer multi-inheritance fun. more c++ multiple inheritance fun This is a
Possible Duplicate: Pointer Arithmetic In C Code: int main() { int a[ ] ={0,1,2,3,4};
Possible Duplicate: Getting Filename from file descriptor in C Obtain filename from file pointer
Possible Duplicate: Test for void pointer in C++ before deleting Is code snippet 1
Possible Duplicate: Why do I get a null pointer exception from TabWidget? I have
Possible Duplicate: casting unused return values to void I read some source code, and
Possible Duplicate: Is it safe to delete a void pointer? Will the following code
Possible Duplicate: Different answers from strlen and sizeof for Pointer & Array based init
Possible Duplicate: Modifying a const through a non-const pointer I have the following code:
Possible Duplicate: checking if pointer points within an array If I have an array

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.