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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:39:44+00:00 2026-06-02T20:39:44+00:00

I came across an instance the other day where I had a function taking

  • 0

I came across an instance the other day where I had a function taking a pointer to a base type which I then needed to up-cast to a derived type to access some additional functionality. However, dynamic_cast failed, which was odd because my type definitely inherited the base type.

To get to the bottom of what was going on, I created the following test program, which I think replicates what I was seeing:

void cast(TestClass *baseType)
{
    if (dynamic_cast<Derived *>(baseType))
        TRACE("cast was sucessful");
    else
        TRACE("cast failed");
}

int main(int argc, char *argv[])
{
    Derived *test1 = new Derived();
    TestClass *test2 = new TestClass();
    TestClass test3;

    test1->identify(); // prints: this is a Derived class
    test2->identify(); // prints: this is a TestClass

    cast(test1); // succesful
    cast(test2); // fail - expected

    // reassign test2 to test1
    test2 = test1;
    test2->identify(); // prints: this is a Derived class

    cast(test2); // succesful

    // the interesting part, the test3 object (created on stack), does not cast
    // despite that it would seem possible from the cast method.
    test3 = *test1;
    test3.identify(); // prints: this is a TestClass
    cast(&test3); // fails?

    return a.exec();
}

Which is interesting, because if you were presented with only the method I called cast(), you would expect it to be possible to cast the object passed in. I’ve demonstrated that this isn’t the case; it depends on how the object is originally created. What is confusing, is why it is possible to cast an object which has been reassigned by reference but not by value. Furthermore, would using static_cast work, as long as we guarantee the types are compatible?

  • 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-02T20:39:46+00:00Added an answer on June 2, 2026 at 8:39 pm

    test3 is of type TestClass (which I assume is the parent of Derived), so the dynamic cast fails.

    Even though you assign *test1 to it, the assignment only copies the TestClass part (aka slicing). When you assign a pointer to a pointer, no slicing occurs.

    You can think of derived objects as having a part of their base:

    *test1:
    |--------------|
    |TestClass part|
    |--------------|
    |Derived part  |
    |--------------|
    
    test3:
    |--------------|
    |TestClass part|
    |--------------|
    

    When you assign a pointer (test2=test1), the object itself does not change, you are just looking at it via a different glass (through a pointer to TestClass), thus casting works.

    When you assign an object itself (test3=*test1), the destination (test3) has room only for a TestClass object, so the copy takes away the extra Derived part.

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

Sidebar

Related Questions

I came across the Dell Studio One 19 on the web the other day,
The other day I came across an alternative way of accessing web.config configuration in
So I came across an instance where a stored procedure which handled the Updates
I came across something very basic but extremely bewildering today. I needed to convert
I came across a few articles like this one , which suggest that some
We came across a bunch of pre-defined Build Macros for instance $(SolutionDir), but can
I came across the following issue: I have a main simple page on which
While reading Jack Shirazi's 'Java performance tuning', I came across the following statement: Instance
I came across a very funny situation where comparing a nullable type to null
Came across something like this today, and was wondering if there was an equivalent

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.