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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:44:16+00:00 2026-05-18T21:44:16+00:00

Suppose we have the following code: #include <iostream> struct A { virtual void f()

  • 0

Suppose we have the following code:

#include <iostream>

struct A
{
    virtual void f() { 
        std::cout << "A::f()" << std::endl;
    }
};

struct B: A
{
    void f() { 
        std::cout << "B::f()" << std::endl;
    }
};

void to_A(void* voidp) {
    A* aptr = static_cast<A*>(voidp);
    aptr->f();
}

void to_B(void* voidp) {
    B* bptr2 = static_cast<B*>(voidp);
    bptr2->f();
}

int main() {
    B* bptr = new B;
    void* voidp = bptr; 
    to_A(voidp); // prints B::f()
    to_B(voidp); // prints B::f()
}

is this code guaranteed to always work as in the code comments or is it UB? AFAIK it should be ok, but I’d like to be reassured.

EDIT
Ok, so it seems there’s a consensus that this code is UB, as one can only cast to the exact type. So, what if the main() changes to:

int main() {
    B* bptr = new B;
    to_A(static_cast<A*>(bptr)); // still prints B::f()
    to_B(bptr); // still prints B::f()
}

is it still UB?

  • 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-18T21:44:16+00:00Added an answer on May 18, 2026 at 9:44 pm

    Your first code example invokes undefined behaviour.

    You can use a static_cast to reverse a standard conversion of pointer to object type to pointer to void but the result is only guaranteed if the value of the pointer to void being converted back to the original object type is the result of the standard conversion of a pointer to the original type to pointer to void.

    Your second code example is OK because you only reverse conversions from pointer-to-type to pointer-to-void by casting back to the original type that the conversion was made from. This is guaranteed in 5.2.9 [expr.static.cast] of the standard (C++03).

    … A value of type pointer to object converted to “pointer to cv void” and back to the original pointer type will have its original value.

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

Sidebar

Related Questions

Suppose you have the following situation #include <iostream> class Animal { public: virtual void
I have the following code taken from cplusplus.com : // set_terminate example #include <iostream>
Suppose that we have the following bit of code: #include <pthread.h> #include <stdio.h> #include
Suppose I have the following code: class some_class{}; some_class some_function() { return some_class(); }
Suppose I have the following code: class siteMS { ... function __CONSTRUCT() { require
Suppose I have the following C code. unsigned int u = 1234; int i
suppose you have the following structure: #include <windows.h> // BOOL is here. #include <stdio.h>
Suppose you have the following C code. unsigned char a = 1; printf(%d\n, ~a);
Your help would be greatly appreciated I have the following code: It is suppose
I have the following code: #include <irrlicht.h> using namespace irr; using namespace core; using

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.