Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > dynamic_cast<>

Reply
Thread Tools

dynamic_cast<>

 
 
alg
Guest
Posts: n/a
 
      07-14-2003
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?


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-14-2003

"alg" <> wrote in message
news:2OrQa.53769$...
> 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.


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-14-2003
On Mon, 14 Jul 2003 06:09:02 GMT, "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.


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.

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      07-14-2003
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.

 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57