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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:55:51+00:00 2026-06-07T18:55:51+00:00

-EDIT- Thanks for the quick response, I’d been having really weird problems with my

  • 0

-EDIT-

Thanks for the quick response, I’d been having really weird problems with my code and I changed my casts to dynamic_cast and its working perfectly now

-ORIGINAL POST-

Is it safe to cast a pointer of one base class to another base class? To expand on this a bit, will the pointer I’ve marked in the following code not result in any undefined behavior?

class Base1
{
public:
   // Functions Here
};


class Base2
{
public:
   // Some other Functions here
};

class Derived: public Base1, public Base2
{
public:
  // Functions
};

int main()
{
  Base1* pointer1 = new Derived();
  Base2* pointer2 = (Base2*)pointer1; // Will using this pointer result in any undefined behavior?
  return 1;
}
  • 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-07T18:55:52+00:00Added an answer on June 7, 2026 at 6:55 pm

    Will using this pointer result in any undefined behavior?

    Yes. The C-style cast will only try the following casts:

    • const_cast
    • static_cast
    • static_cast, then const_cast
    • reinterpret_cast
    • reinterpret_cast, then const_cast

    It will use reinterpret_cast and do the wrong thing.

    If Base2 is polymorphic, i.e, has virtual functions, the correct cast here is dynamic_cast.

    Base2* pointer2 = dynamic_cast<Base2*>(pointer1);
    

    If it doesn’t have virtual functions, you can’t do this cast directly, and need to cast down to Derived first.

    Base2* pointer2 = static_cast<Derived*>(pointer1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: Thanks for the quick answers. Please mark this question for deletion. It doesn't
Why new()/delete() is slower than malloc()/free()? EDIT: Thanks for the answers so far. Please
EDIT: SOLVED Thanks Brooks. Your question led me to keep digging into if the
Please let me know if you have any idea about it. Thanks EDIT What
EDIT: I figured it out from Bergi's answer in the end. Thanks Bergi. pubPrivExample
UPDATE: Solved. Thanks BusyMark! EDIT: This is revised based on the answer below from
Code as reference: http://jsbin.com/aboca3/2/edit In this example above (thank you SLaks) I am truncating
I am trying to create a quick fix for a problem I am having.
Hello there and thanks in advance for any help on the following matter. Edit:
EDIT: Thanks guys - perfect! I wish I could check more than one answer,

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.