Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > query on dynami...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 9562 of 9593
Post > Topic >>

query on dynamic cast

by Bharath <tiromarch08@[EMAIL PROTECTED] > Apr 30, 2008 at 03:56 AM

Hello All,
I was trying the below example for which I have a doubt. My doubt is
that why is virtual needed for function "dummy" in class CBase which
makes program compile?
Secondly, As per my current understanding of dynamic cast, pba is a
object of CDerived which RTTI knows and hence cast should be
successful. Isn't it? Which concept I didn't understand here ?

//Program to understand the dynamic cast.
#include <iostream>
#include <exception>
using namespace std;

class CBase
{
     void dummy() {}
};
class CDerived: public CBase
{
};

int main () {
   try {
     CBase * pba = new CDerived;
     CDerived * pd;

     pd = dynamic_cast<CDerived*>(pba);
     if (pd==0) cout << "Null pointer on first type-cast" << endl;
   } catch (exception& e) {cout << "Exception: " << e.what();}
   return 0;
}

Thanks.

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]




 5 Posts in Topic:
query on dynamic cast
Bharath <tiromarch08@[  2008-04-30 03:56:04 
Re: query on dynamic cast
Francis Glassborow <fr  2008-04-30 10:27:43 
Re: query on dynamic cast
Tony Delroy <tony_in_d  2008-04-30 10:27:06 
Re: query on dynamic cast
Carl Barron <cbarron41  2008-04-30 10:27:35 
Re: query on dynamic cast
Thomas Maeder <maeder@  2008-04-30 10:43:22 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed May 14 6:44:44 CDT 2008.