Thread: Dynamic Casting Problem

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    103

    Dynamic Casting Problem

    Hi

    I was trying to learn dynamic casting and saw some strange behavior.
    My program was crashed as exception is thrown as bad typeid.

    Below is code:
    Code:
    #include<iostream>
    #include <exception>
    
    using namespace std;
    
    class base
    {
    public:
     virtual void show()
    	{
    		cout<<"inside base";
    	}
    };
    
    class derived: public base
    {
    public:
    	void show()
    	{
    		cout<<"inside derived";
    	}
    };
    
    int main()
    {
       //base *a;
        derived *d;
    	try
    	{
           base *a = dynamic_cast<derived*>(d);
    	   throw bad_typeid();
    	 }
    	catch(...)
    	{
    		cout<<"bad_typeid";
    	}
    I know that we do upcasting as derived is inherited from base.
    So why this is failing. Can any body le me know what I am missing here?

    Thanks
    Nickman

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Look at line 31. Is there any way that a bad_typeid would NOT be thrown?

    The dynamic_cast you're doing converts a derived * into a derived *. That does nothing. Similarly, a derived * is implicitly converted to a base *.

    Unrelated to the problem you asked about, but bad_typeid is specified as being declared declared in the <typeinfo> header, not <exception> or <typeinfo>.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also, d is pointing to nowhere valid.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A casting problem?
    By uderman in forum C Programming
    Replies: 2
    Last Post: 03-07-2009, 12:35 PM
  2. Casting problem
    By Wilhelmina in forum C++ Programming
    Replies: 1
    Last Post: 07-19-2007, 09:03 AM
  3. casting problem
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 12-30-2006, 07:11 AM
  4. Casting Problem
    By Sebastiani in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2002, 02:06 PM
  5. I am having a problem with casting
    By blackwyvern in forum C++ Programming
    Replies: 4
    Last Post: 03-14-2002, 02:53 PM