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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:54:42+00:00 2026-05-19T02:54:42+00:00

In the following code, while constructing obj in case 1, we construct a derived

  • 0

In the following code, while constructing obj in case 1, we construct a derived class object too, but its member functions are just inaccessible to obj. So while downcasting (i.e., in case 2), using obj as source, we have the constructed derived in it already. Why would obj need to be polymorphic?

If I confused you with my above description, why doesn’t obj need to be polymorphic when upcasting, but while downcasting it does need to be polymorphic while using dynamic_cast?

class base
{
public:
    base()
    {
        cout<< " \n base constructor \n";
    }
};

class derived : public base
{
public:
    derived()
    {
        cout << " \n derived constructor \n";
    }
};

base *obj = dynamic_cast<base*> (new derived); // case 1: explicitly upcasting
derived *OBJ = dynamic_cast<derived*> (obj);   // case 2: error
  • 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-19T02:54:42+00:00Added an answer on May 19, 2026 at 2:54 am

    From 5.2.7/1 [expr.dynamic.cast] :

    The result of the expression dynamic_cast<T>(v) is the result of converting the expression v to type
    T.

    […]

    If T is “pointer to cv1 B” and v has type “pointer to cv2 D” such that B is a base class of D, the result is a
    pointer to the unique B sub-object of the D object pointed to by v.

    […]

    Otherwise, v shall be a pointer to or an lvalue of a polymorphic type.

    The standard even provides the following example which illustrates that the polymorphic type requirement does not stand for derived to base conversion :

    struct B {};
    struct D : B {};
    void foo(D* dp)
    {
        B* bp = dynamic_cast<B*>(dp); // equivalent to B* bp = dp;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code illustrates an object literal being assigned, but with no semicolon afterwards:
The following code causes a SIGSEGV, but only while debugging. #include <stdio.h> #include <stdlib.h>
Given the following code (that doesn't work): while True: # Snip: print out current
Consider the following code: client.Send(data, data.Length, endpoint); byte[] response = client.Receive(ref endpoint); While, according
I noticed for a while now the following syntax in some of our code:
The following code works great in IE, but not in FF or Safari. I
The following code doesn't compile with gcc, but does with Visual Studio: template <typename
i've got the following code :- while (....) { var foo = DoTheFooShakeShakeShake(..); foos.Add(foo);
I had the following code in a test.I am confused about what (i,j) evaluates,while
I have the following code (taken from actual script): $result=mysql_query($sql); while($row=mysql_fetch_assoc($result)) { $auth_token=$row['oauth_token']; $auth_token_secret=$row['oauth_token_secret'];

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.