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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:57:38+00:00 2026-06-02T15:57:38+00:00

I was reading on this page and it says dynamic_cast can be used only

  • 0

I was reading on this page and it says

dynamic_cast can be used only with pointers and references to objects. Its purpose is to ensure that the result of the type conversion is a valid complete object of the requested class.

But dynamic_cast doesn’t give a valid object when I do this:

struct A{};
struct B:A{};
void main(){
    A a;
    B b;
    B* bPtr = (B*) 0x0000001;
    A* aPtr = dynamic_cast<A*>(bPtr);
    //Now aPtr is the memory address 0x00000001
}

Since the bPtr does not contain a valid A object, I would expect dynamic_cast to fail this cast so that aPtr would get NULL. But this is not happening? Which one is wrong, the compiler (g++ 4.5.7) or the above mentioned web page? Or am I missing something about what dynamic_cast is actually supposed to do?

Edit: Answer Summary

  1. In upcast, dynamic_cast does nothing. There is no guarantee of any sort.
  2. In downcast, dynamic_cast does not check for bad pointers. It assumes that the source pointer either is 0 or points to a valid source object. (the dynamic_cast operation can segfault during a downcast if it is given a bad pointer)
  3. Downcast through dynamic_cast requires at least one virtual member in the base class.
  • 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-02T15:57:39+00:00Added an answer on June 2, 2026 at 3:57 pm

    dynamic_cast<> is guaranteed to give a valid object, provided that it’s used properly.

    The very 1st requirement to use this cast is to have polymorphic base class (at least 1 virtual function). In your example, A and B are not polymorphic and it will result in compilation error.

    The 2nd requirement is that dynamic_cast<> should be used for downcasting, in your case you are using it for upcast (which happens trivially).

    Suppose, they were polymorphic then the 3rd requirement is to use this casting on a pointer/reference to a proper object.
    bPtr = (B*)0x0000001 is not guaranteed to refer an object (mostly it’s a UB) of B.

    Here is the correct example:

    struct A{ virtual ~A() {} };
    struct B : A {};
    void main(){
      A* pA = new B;
      B* pB = dynamic_cast<B*>(pA);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading this page , it says: ...the URI in a PUT request identifies the
I am reading this page : http://freespace.virgin.net/hugo.elias/models/m_perlin.htm where the following function is used to
i am reading this page http://www.cplusplus.com/doc/tutorial/exceptions.html it says if i write function() throw(); no
I'm reading the adaptive paypals API ebook and it says on page 21, that
I was reading this page and I found the following statement: MVC in Java
I'm reading a web page using HttpClient like this: httpclient = new DefaultHttpClient(); httpget
Reading this article http://support.microsoft.com/kb/813878 I have a question: Where can I get ipseccmd.exe for
While reading this class BitmapFactory I noticed that almost all methods inside are static.
After reading this article on thedailywtf.com, I'm not sure that I really got the
I'm reading about using git as an svn client here: http://learn.github.com/p/git-svn.html That page suggests

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.