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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:24:33+00:00 2026-06-10T09:24:33+00:00

This is a different question than the other one I’ve posted, I have this

  • 0

This is a different question than the other one I’ve posted, I have this piece of code:

class Base
{
public:
    Base()
    {

    };


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

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


class Derived2 : public Derived
{
public:

    Derived2()
    {

        cout << "Derived2 constructor";
    }
    void Method()
    {
        cout << "Override2 Method";
    }
};

int main()
{

    Base *myPointer = new Derived();

    dynamic_cast<Derived2*>(myPointer)->Derived2::Method();

    Sleep(700);

    delete myPointer;

    return 0;
}

If I write

dynamic_cast<Derived2*>(myPointer)->Method();

there’s a failure (dynamic_cast returns NULL and NULL->Method() provokes an exception) and this is what I was expecting, but if I write

dynamic_cast<Derived2*>(myPointer)->Derived2::Method();

the function succeeds without even calling the Derived2 constructor. Method isn’t even a static function, what is going on here?

  • 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-10T09:24:34+00:00Added an answer on June 10, 2026 at 9:24 am

    You triggered undefined behavior by calling a member function on a NULL pointer. If you use dynamic_cast, you must either check the returned pointer for NULL before dereferencing it or 100% ensure you never cast to a type that is not the type of the object being cast or one of its parents.

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

Sidebar

Related Questions

This is more of a architect question rather than one that requires code. I
I've seen this question asked on here, but it seems to be different than
This is a little different than the questions that have already been asked on
I'm re-asking this question but with a different framework this time. I have two
This question is moot - see @Alochi comments. my bad. This is different than
this is a different question concerning: add a connection to database not working, asp.net
[Please note that this is a different question from the already answered How to
This is similar to (but different from) this question . Here is some simple
Similar, but different to this question : I use highlight occurrences a lot, but
I asked a question about different testing frameworks yesterday. This question can be found

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.