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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:09:15+00:00 2026-06-05T07:09:15+00:00

If we use multiple inheritance, slicing will make the addresses to parent objects differ

  • 0

If we use multiple inheritance, slicing will make the addresses to parent objects differ from the address to leaf objects:

struct X {int x};
struct Y {int y};
struct Z : X, Y {int z};

So if we have a Z object z, its address &z will not coincide with the address of its Y parent: static_cast<Y*>(&z) is four bytes higher than &z.

The good thing about static_cast is that it’s, well, static, so doesn’t take up runtime (compared to dynamic_cast, that is).
However, if we have a Z* that points at 0, every cast to a parent should and does yield a null pointer as well.
Why does this work and how is it implemented? Does that imply that every single static_cast introduces a branch instruction?

  • 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-05T07:09:16+00:00Added an answer on June 5, 2026 at 7:09 am

    Yes, both an implicit conversion from a pointer to a derived class to a pointer to a base class and a static_cast back again must preserve null pointer values. This means that a branch is usually required in the generated code for multiple inheritance cases where the base class address does not coincide with the derived class address.

    It’s theoretically possible for an implementation to reserve a range of addresses around a ‘zero’ address to represent null pointers and avoid the branch in this case but it would be at the expense of adding extra checking for comparisons of null pointers.

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

Sidebar

Related Questions

Is it OK to use multiple inheritance if one parent class is an interface
Say we have a class inheriting from two base classes (multiple inheritance). Base class
To follow from my previous question about virtual and multiple inheritance (in a cross
I would like to make use of few methods from couple of my old
Possible Duplicate: Why to use Interfaces ? Multiple Inheritance vs Interfaces ? Benefits of
Whats the other option we can use for multiple inheritance other than implementing interface
Can anyone think of any situation to use multiple inheritance? Every case I can
I understand multiple inheritance and interface to an extent. Is it possible to use
I ask myself how use multiple images in one. For example youtube -> http://s.ytimg.com/yt/img/master-vfl102488.png
I need to use multiple hosts under IIS for WCF. We're using wshttpbinding and

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.