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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:12:30+00:00 2026-05-27T06:12:30+00:00

The following code seems to work with the compilers (clang, g++ on both, Linux

  • 0

The following code seems to work with the compilers (clang, g++ on both, Linux and Mac OS) I tried, but is it guaranteed to always do what one would expect?

struct A {
  virtual void foo() = 0;
};

struct A2 {
  virtual void foo() = 0;
};

struct B : public A2 {
  void foo() {
    printf("test\n");
  }
};

int main() {
  B* b = new B;
  ((A*)b)->foo();
}

I realize this is bad practice and one should not do this, but does it work generally?

  • 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-27T06:12:31+00:00Added an answer on May 27, 2026 at 6:12 am

    It’s not bad practice: It doesn’t work. It will do something allright. Not unlikely, crashing. It is all allowed since you are invoking unspecified behaviour. Edit You might consult your compiler technical documentation (refer to ABI) to find compiler-specific extensions that you might rely on.

    Try using

    static_cast<A*>(b)    // invalid static cast (compile error)
    dynamic_cast<A*>(b)   // returns null pointer value (runtime)
    

    What you are doing is effectively

    • reinterpret_cast<A*>(b)

    and the results are entirely your own responsibility implementation-defined.

    Edit To Nawaz: relevant standards passage: § 5.2.10, clause

    7. A pointer to an object can be explicitly converted to a pointer to a different object type.69 When a
    prvalue v of type “pointer to T1” is converted to the type “pointer to cv T2”, the result is static_cast<cv T2*>(static_cast<cv void*>(v)) if both T1 and T2 are standard-layout types (3.9) and the alignment
    requirements of T2 are no stricter than those of T1. Converting a prvalue of type “pointer to T1” to the type “pointer to T2” (where T1 and T2 are object types and where the alignment requirements of T2 are no stricter than those of T1) and back to its original type yields the original pointer value. The result of any
    other such pointer conversion is unspecified
    .

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

Sidebar

Related Questions

I have the following code in a shell script. This only seems to work
Hey guys, the following snippet of jQuery code seems to work fine in Google
I have absolutely no idea why this is happening but the following code seems
I have the following code that seems like it should set my insertParameter but
I have the following code but the lon/lat seems to be returning null; package
In the following code, both amp_swap() and star_swap() seems to be doing the same
The following code seems to be just too much, for getting a single count
In the following code, it seems class C does not have access to A's
I'm comparing the following code in C++ and C# and C# (Mono 2.4) seems
When I run the following code, it only seems to be downloading the first

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.