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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:02:10+00:00 2026-05-24T08:02:10+00:00

A tweak on this question that I’ve run into. Consider: class A {}; class

  • 0

A tweak on this question that I’ve run into. Consider:

class A {};

class B : private A {
   static void foo();
};

void B::foo(){
   B* bPtr1 = new B;
   A* aPtr1 = dynamic_cast<A*>(bPtr1); // gives pointer
   B* bPtr2 = dynamic_cast<B*>(aPtr1); // gives NULL
}

Since aPtr1 is, in fact, of type B*, and since we’ve got full access to B and its inheritance from A, I’d expected both casts to work. But they don’t; why? Is there another way to achieve this cast?

Note that:

  • If foo() were not a member of B, both casts would fail.
  • If B inherits from A publicly, both casts would work.
  • 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-24T08:02:11+00:00Added an answer on May 24, 2026 at 8:02 am

    5.2.7 (ISO/IEC 14882, 12/29/2003) is pretty explicit on this point:

    [about the expression dynamic_cast<T>(v)]

    If T is “pointer to cv1 B” and v has type “pointer to cv2
    D” such that B is a base class of D, the result is a
    pointer to the unique B sub-object of the D object pointed to by v.
    [… bla bla about cv1 and cv2 …] and B shall be an accessible unambiguous base class
    of D
    (emphasis mine)

    (recall 11.2 “A base class is said to be accessible if an invented public member of the base class is accessible.”).

    This explains why the first cast works. Now, for the second:

    […]

    Otherwise, a run-time check is applied to see if the object pointed or
    referred to by v can be converted to the type pointed or referred to
    by T.

    The run-time check logically executes as follows:

    • If, in the most derived object pointed (referred) to by v, v points
      (refers) to a public base class subobject of a T object, and if only
      one object of type T is derived from the sub-object pointed (referred)
      to by v, the result is a pointer (an lvalue referring) to that T
      object.
    • Otherwise, if v points (refers) to a public base class sub-object
      of the most derived object, and the type of the most derived object
      has a base class, of type T, that is unambiguous and public, the
      result is a pointer (an lvalue referring) to the T sub-object of the
      most derived object.
    • Otherwise, the run-time check fails.

    The value of a failed cast to pointer type is the null pointer value
    of the required result type. A failed cast to reference type throws
    bad_cast (18.5.2).

    So it seems that the behavior you observe is due to the private inheritance: even if the base class is accessible, it is not public, and the standard requires public, not accessible.

    Annoying, isn’t it ? I don’t have the C++0x draft handy, perhaps someone can edit my answer with quotes from it, in case things have changed.

    Is there another way to achieve this cast?

    It depends on what you want to do. Basically, private inheritance is just another device for performing composition. If you really are to return a pointer to the private derived instance, then either make the inheritance public, or return a member.

    Anyways, you’ll happy to know that static_cast doesn’t seem to have this limitation:

    5.2.9. [about static_cast<T>(v)] […]

    An rvalue of type “pointer to cv1 B”, where B is a class type, can be converted to an rvalue of type “pointer
    to cv2 D”, where D is a class derived (clause 10) from B, if a valid standard conversion from “pointer to D”
    to “pointer to B” exists
    (4.10), cv2 is the same cv-qualification as, or greater cv-qualification than, cv1, and
    B is not a virtual base class of D. The null pointer value (4.10) is converted to the null pointer value of the
    destination type. If the rvalue of type “pointer to cv1 B” points to a B that is actually a sub-object of an
    object of type D, the resulting pointer points to the enclosing object of type D. Otherwise, the result of the
    cast is undefined.

    so if you know for sure what the actual dynamic type of the pointer is, you are allowed to static_cast inside foo.

    I’d be interested in any additional information about why this inconsistency exists.

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

Sidebar

Related Questions

I'm extremely new to JS and have this code that I'm trying to tweak.
this is more a conceptual question. Consider you have a php framework that runs
Sorry, this might be a basic/stupid/noob question - I am just trying to tweak
I realize that this is probably a very basic question, but I have spent
I know this is probably a simple question, but I'm attempting a tweak in
Hi I am hoping that somebody can help me to tweak this JQuery code
I'm trying to tweak code that rendered by Glimmer which probably marks my CSS
Is there a preference or tweak that will stop Eclipse from putting up a
Slashdot has a little widget that allows you to tweak your comment threshold to
This is sort of the next step of the LINQ to DB2 question I

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.