By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,110 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,110 IT Pros & Developers. It's quick & easy.

dynamic_cast<>

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a

"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.