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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:10:11+00:00 2026-06-10T11:10:11+00:00

I have some doubts about static_cast and dynamic_cast in C++. Do they completely change

  • 0

I have some doubts about static_cast and dynamic_cast in C++. Do they completely change the object a pointer is pointing to from a class A to a class B by preserving the already-set member variables (except the ones that cannot be passed from derived to base) ?

I noticed that if I have something like

struct Base
{
    Base() { }
    virtual ~Base() { }
    virtual void Method() { cout << "Base Method"; }
};

class Derived : public Base
{
public:
    virtual void Method() { cout << "Override Method"; }
};

struct Derived2 : public Derived
{
    Derived2() { cout << "Derived2 constructor"; }
    void Method() { cout << "Override2 Method"; }
};

int main()
{       
    Base *myPointer = new Derived();    
    static_cast<Derived2*>(myPointer)->Derived2::Method();   
    delete myPointer;    
    return 0;
}

The constructor isn’t called, but the method does. How is this possible?

  • 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-10T11:10:12+00:00Added an answer on June 10, 2026 at 11:10 am

    The casts don’t change the object at all. They only give you a different pointer to a related class type in the inheritance hierarchy:

    Derived x;
    
    Base         * p = &x;
    AnotherClass * q = dynamic_cast<AnotherClass*>(p);
    
    // q may or may not be NULL
    

    For example, the above dynamic cast succeeds if we have a hierarchy AnotherClass : Base and Derived : AnotherClass (and Base is polymorphic).

    A static_cast can usually be used when you already know that you have a more derived dynamic type, but happen to have only a pointer or reference to a base.

    (A static cast can never be used to cast from a virtual base, in which case you always need dynamic_cast.)

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

Sidebar

Related Questions

I have some doubts about construction and initialization order guarantees in C++. For instance,
I'm a beginner about javascript, and I have some doubts about the variables of
I have programmed an emulator, but I have some doubts about how to organizate
I have some doubt about python's class variables. As my understanding, if I define
I have some doubts about how to concatenate MvcHtmlString instances because of this information
I have an idea of organising a game loop. I have some doubts about
I have some doubts: I want to show the photos from my company facebook
I've heard recently about the Abstract Factory Pattern and have currently some doubts on
I have some doubts about this server response stream compression on MVC. Actually im
morning I have some doubts about the the way php works. I cant find

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.