473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

problem in dynamic cast

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

Sep 1 '05 #1
5 7981
> HI,
Are ther any pitfalls for dynamic cast in type safe downcasting..


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

Sep 1 '05 #2
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.

Sep 1 '05 #3

ri*****@hotmail.com schreef:
One that caught me last week using code from a third party library that
was not compiled with RTTI turned on ("/GR").


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

Sep 1 '05 #4
Radde wrote:
HI,
Are ther any pitfalls for dynamic cast in type safe downcasting..


Performance may be an issue and may be dependant on the complexity of
your class definition.
Sep 1 '05 #5
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

Sep 1 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: vsgdp | last post by:
Must a class have virtual functions to use dynamic cast? If so, and I need to still downcast between a small hierarchy in which there are no methods, I should use static_cast ?
5
by: AriZOnA | last post by:
Hi! I have the following scenario: class QWidget {} class GeneratedClass : public QWidget {} class AbstractPanel
3
by: Ole Hanson | last post by:
Hi Trying to cast an exception to the correct type, I encounter some "strange" experiences: The compiler complains that the variable "t" is unknown in line 2 (Exception trueException =...
2
by: Calum Grant | last post by:
Here's a little utility I thought I'd share. (For those familiar with ATL::CComQIPtr, the concept is similar). ==== #ifndef DYNPTR_H_INCLUDED #define DYNPTR_H_INCLUDED
13
by: Ninan | last post by:
struct A { int a; int b; virtual ~A(); } struct B : public A { int c;
4
by: Zark3 | last post by:
Hi all, I was wondering if anybody could enlighten me on the possibility of dynamic casting. Or, well, whether or not I'm actually trying to do this the right way. What I have is a base class...
11
by: axel22 | last post by:
Please observe this simple model of multiple inheritance: void main() { class A { public: virtual void print() { cout << "A" << endl; }; class Support1 : virtual public A {
6
by: Nick Keighley | last post by:
Hi, Is this code fundamentally broken? class B { } class D: public B {
6
by: puzzlecracker | last post by:
What about dynamic_cast when dynamic reference is not unique (e.g. A- Why would you use dynamic cast<void *>? What is typeid & what does it return? How would you use it? What is wrong here ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.