Talk About Network

Google


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 > Re: query on dy...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 5 Topic 9562 of 9823
Post > Topic >>

Re: query on dynamic cast

by Tony Delroy <tony_in_da_uk@[EMAIL PROTECTED] > Apr 30, 2008 at 10:27 AM

On Apr 30, 6:56 pm, Bharath <tiromarc...@[EMAIL PROTECTED]
> wrote:
> [...]
> 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;
> }

( presumably, the problem was that the "Null pointer on first type-
cast" message was being printed unexpectedly )

RTTI is a mechanism sup****ting cl***** employing virtual dispatch.
Just make a member of CBase virtual (even dummy() will do), and the
dynamic_cast<> will start working.

Cheers, Tony

-- 
      [ 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 Thu Jul 24 1:47:32 CDT 2008.