Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 1st, 2005, 05:35 AM
Radde
Guest
 
Posts: n/a
Default problem in dynamic cast

HI,
Are ther any pitfalls for dynamic cast in type safe downcasting..

  #2  
Old September 1st, 2005, 10:05 AM
Srini
Guest
 
Posts: n/a
Default Re: problem in dynamic cast

> HI,[color=blue]
> Are ther any pitfalls for dynamic cast in type safe downcasting..[/color]

What exactly do you mean by "pitfalls"?

dynamic_cast is a runtime casting operator. Because the cast is
evaluated at runtime, it is probable that the cast may fail even though
the code compiles. In the case of casting object pointers, the failed
cast will result in a NULL pointer. In the case of casting object
references, the failed cast will result in throwing a bad_cast
exception object. It is very important to note that casting across or
down the class hierarchy using
dynamic_cast is only possible when the base class is a polymorphic
type, that is, the class has at least one virtual method.

Regards,
Srini

  #3  
Old September 1st, 2005, 11:15 AM
ringofw@hotmail.com
Guest
 
Posts: n/a
Default Re: problem in dynamic cast

One that caught me last week using code from a third party library that
was not compiled with RTTI turned on ("/GR"). dynamic_cast of a pointer
to such an object (i.e. which should not throw a bad_cast exception, so
it wasn't in a try block) can throw an std::exception with the
following "what".

"Access violation - no RTTI data!"


Proviso: bits of the above may be Microsoft-specific.

  #4  
Old September 1st, 2005, 11:25 AM
msalters
Guest
 
Posts: n/a
Default Re: problem in dynamic cast


ringofw@hotmail.com schreef:
[color=blue]
> One that caught me last week using code from a third party library that
> was not compiled with RTTI turned on ("/GR").[/color]

That makes it not C++, and thus off-topic. C++ has RTTI, C doesn't and
there are literally thousands of bastards inbetween without. In
general,
this group doesn't deal with these bastards.

HTH,
Michiel Salters

  #5  
Old September 1st, 2005, 02:15 PM
Gianni Mariani
Guest
 
Posts: n/a
Default Re: problem in dynamic cast

Radde wrote:[color=blue]
> HI,
> Are ther any pitfalls for dynamic cast in type safe downcasting..
>[/color]

Performance may be an issue and may be dependant on the complexity of
your class definition.
  #6  
Old September 1st, 2005, 02:55 PM
Stuart MacMartin
Guest
 
Posts: n/a
Default Re: problem in dynamic cast

As a general rule, you should not use dynamic_cast in MFC code. Only
use dynamic_cast in MFC code where the code cannot possibly be a
performance bottleneck and even then test your code that it behaves in
a reasonable amount of time. The excessive string compares typically
will grind your application to a halt.

Stuart

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles