dynamic_cast<>

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?
 
J

John Harrison

alg said:
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.
 
A

Alf P. Steinbach

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.
 
R

Rolf Magnus

alg said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top