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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:16:56+00:00 2026-06-09T14:16:56+00:00

I am curious as to how casting pointers to derived and base-classes actually works.

  • 0

I am curious as to how casting pointers to derived and base-classes actually works.
Here is an example:

struct A {};
struct B : A {};

// Boxing a pointer to an instance of B
void* p = new B();

Now, let´s say I want to access possible members or methods of A through the pointer p.

A* a1 = (A*)p;
A* a2 = (A*)((B*)p);

Which one is correct? Is there a difference at all?
Could you tell me where I can get further information on this subject?

  • 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-09T14:16:57+00:00Added an answer on June 9, 2026 at 2:16 pm

    In this case, no difference in practice.

    But there could be difference if there is multiple inheritance:

    #include <cstdio>
    
    struct A { int x; };
    struct B { int y; };
    
    struct C : B, A {};
    
    int main() {
        void* c = new C();
        printf("%p\n", c);             // 0x1000
        printf("%p\n", (A*) c);        // 0x1000
        printf("%p\n", (A*) ((C*) c)); // 0x1004
        return 0;
    }
    

    or the subclass has a virtual method but the parent does not[1], including using virtual inheritance[2].

    In terms of the standard, as OP uses C-style cast, which in this case is equivalent to static_cast.

    The cast sequence B* → void* → B* → A* is valid, where the first two cast will return back the same pointer as required by §5.2.9[expr.static.cast]/13, and the last cast works as a pointer conversion §4.10[conv.ptr]/3.

    However, the cast sequence B* → void* → A* will actually give undefined result, because, well, the result is not defined in §5.2.9/13 ☺.


    [1]:

    #include <cstdio>
    
    struct A { int x; };
    struct C : A { virtual void g() {} };
    
    int main() {
        void* c = new C();
        printf("%p\n", c);              // 0x1000
        printf("%p\n", (A*) c);         // 0x1000
        printf("%p\n", (A*) ((C*) c));  // 0x1008
        return 0;
    }
    

    [2]:

    #include <cstdio>
    
    struct A { int x; };
    struct C : virtual A {};
    
    int main() {
        void* c = new C();
        printf("%p\n", c);              // 0x1000
        printf("%p\n", (A*) c);         // 0x1000
        printf("%p\n", (A*) ((C*) c));  // 0x1008
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Curious, what happens when you return keyword this from a struct in C#? For
Curious to know how sendBroadcast works internally, and if someone could please explain that
Curious about user notification techniques. Referring to simple stuff. For example, a user submit's
Curious about how the handle STDERR works? Lets keep it down to say Batch
Curious if anything jumps out here. But when I run this code block :
curious what the BSON::ObjectId.id.data array represents? for example [77, 145, 20, 13, 225, 96,
Just curious, Can you fake an uptime , for instance increase it to 10
Curious what I'm doing wrong here : employee_ids = $('[data-employee_id='+employee+']'); timestamp_ids = $('[data-scheduled_on='+timestamp+']'); var
Curious about running multiple xvfb displays: I have between 10-50 instances of a script
Curious whether folks have setup 2 way transactional replication on the tables ASP.NET uses

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.