473,383 Members | 1,984 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,383 software developers and data experts.

dynamic_cast<>

alg
dynamic_cast<> comes in play when to perform conversion from a pointer to a
base class to a pointer to a derived class.

I don't understand:

1. why this is so necessary since we can either use an explicit cast or use
"static_cast<>" (Can we?)?
2. the above said conversion is dangerous, using explicit conversion. Does
this mean that using dynamic_cast<> will be safer. How this could be?
3. Is static_cast<> at compiling time and dynamic_cast<> realized at
runtime?
Jul 19 '05 #1
3 7078

"alg" <al******@yahoo.com> wrote in message
news:2O*********************@bgtnsc04-news.ops.worldnet.att.net...
dynamic_cast<> comes in play when to perform conversion from a pointer to a base class to a pointer to a derived class.

I don't understand:

1. why this is so necessary since we can either use an explicit cast or use "static_cast<>" (Can we?)?
See below.
2. the above said conversion is dangerous, using explicit conversion. Does
this mean that using dynamic_cast<> will be safer. How this could be?
Because dynamic_cast will return NULL if the cast cannot be safely made.
static_cast will do the cast anyway. Try this

struct B { virtual ~B() {} };
struct D : B {};

int main()
{
B* b = new B;
D* d1 = static_cast<D*>(b); // unsafe
D* d2 = dynamic_cast<D*>(b); // safe, returns NULL
cout << d1 << ' ' << d2 << endl;
}
3. Is static_cast<> at compiling time and dynamic_cast<> realized at
runtime?


Yes.
Jul 19 '05 #2
On Mon, 14 Jul 2003 06:09:02 GMT, "alg" <al******@yahoo.com> wrote:
dynamic_cast<> comes in play when to perform conversion from a pointer to a
base class to a pointer to a derived class.
And in other situations, but where did you get this sentence?

I don't understand:

1. why this is so necessary since we can either use an explicit cast or use
"static_cast<>" (Can we?)?
A dynamic cast _is_ explicit.

2. the above said conversion is dangerous, using explicit conversion. Does
this mean that using dynamic_cast<> will be safer. How this could be?
3. Is static_cast<> at compiling time and dynamic_cast<> realized at
runtime?


A dynamic cast performs runtime checking whenever needed, but that doesn't
mean that it does so when all necessary type information is available
at compile time, or (especially) when there is no runtime type info.

Do check this out in your documentation or the C++ standard.

Jul 19 '05 #3
alg wrote:
dynamic_cast<> comes in play when to perform conversion from a pointer
to a base class to a pointer to a derived class.

I don't understand:

1. why this is so necessary since we can either use an explicit cast
or use "static_cast<>" (Can we?)?
To find out if the object can really be used as one of the specified
derived class.
Btw, your terminology "explicit cast" is not very good. First, all casts
are explicit, since that's their nature. A cast is what you write into
your code to explicitly invoke a conversion. This of course also means
that dynamic_cast _is_ an "explicit cast", just like all other casts.
2. the above said conversion is dangerous, using explicit conversion.
Casts should be avoided if possible, but if you need a downcast on
polymorphic types, dynamic_cast is the safest one.
Does this mean that using dynamic_cast<> will be safer.
Than what? static_cast? Depending on the situation, it can be safer.
How this could be?
dynamic_cast returns 0 (when used on pointers) or throws an exception
(when used on references) if the object is actually not of the
specified class.
3. Is static_cast<> at compiling time and dynamic_cast<> realized at
runtime?


Yes.

Jul 19 '05 #4

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

Similar topics

11
by: Jamie Burns | last post by:
Hello, I just did a simple benchmark: for (xx=0;xx<100000;xx++) { rDerived* derived = dynamic_cast<rDerived*>(object); if (derived) derived->setValue(message.data.messageSetInt.value); } ...
15
by: Grizlyk | last post by:
Hello. Returning to question of manual class type identification, tell me, for ordinary inheritance is C++ garantee that dynamic_cast<Derived*>(Base*) can be implemented similarly to ...
13
by: baltasarq | last post by:
Hi, there ! When I use dynamic_cast for a single object, I do something like: void foo(Base *base) { if ( dynamic_cast<Derived *>( base ) != NULL ) { ((Derived *) base)->do_something() } }
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.