Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What the hell is dynamic_cast for?

Reply
Thread Tools

What the hell is dynamic_cast for?

 
 
lovecreatesbea...@gmail.com
Guest
Posts: n/a
 
      05-28-2008
Suppose I have the following three classes,

GrandBase <-- Base <-- Child <-- GrandChild

The following cast expression holds true only if pBase points object
of type of ``Child'' or ``GrandChild'', i.e. types not upper than
Child in the above class hierarchy,

dynamic_cast<Child*>pBase

Do I draw this correctly?

Up-casting is safe while down-casting is not. dynamic_cast seems to do
down-casting, so why is dynamic_cast intenting to do the dangerous
work?
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      05-28-2008
(E-Mail Removed) wrote:
> Up-casting is safe while down-casting is not. dynamic_cast seems to do
> down-casting, so why is dynamic_cast intenting to do the dangerous
> work?


Dynamic casting is intended to do the "dangerous work" more safely.
While a static_cast will simply cast to the derived type regardless of
whether the object is of that type, dynamic cast actually checks that
the object is of that type, and if it isn't, it returns a null pointer.
You can check the returned pointer to see if the cast was successful.
(With static_cast you probably just get a segmentation fault.)

There are situation where downcasting may be unavoidable or the path
of least resistance. Thus it's good to have a tool to make it safer.
 
Reply With Quote
 
 
 
 
Fernando Gˇmez
Guest
Posts: n/a
 
      05-28-2008
On May 28, 12:48 pm, Victor Bazarov <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > Suppose I have the following three classes,

>
> > GrandBase <-- Base <-- Child <-- GrandChild

>
> > The following cast expression holds true only if pBase points object
> > of type of ``Child'' or ``GrandChild'', i.e. types not upper than
> > Child in the above class hierarchy,

>
> > dynamic_cast<Child*>pBase

>
> > Do I draw this correctly?

>
> You missed the parentheses and your class has to be polymorphic (have at
> least one virtual function).


Wouldn't that be: "have a vritual destructor"?
 
Reply With Quote
 
David C├┤me
Guest
Posts: n/a
 
      05-28-2008
On Wed, 28 May 2008 21:07:26 +0200, Fernando G├│mez
<(E-Mail Removed)> wrote:

> On May 28, 12:48 pm, Victor Bazarov <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>> > Suppose I have the following three classes,

>>
>> > GrandBase <-- Base <-- Child <-- GrandChild

>>
>> > The following cast expression holds true only if pBase points object
>> > of type of ``Child'' or ``GrandChild'', i.e. types not upper than
>> > Child in the above class hierarchy,

>>
>> > dynamic_cast<Child*>pBase

>>
>> > Do I draw this correctly?

>>
>> You missed the parentheses and your class has to be polymorphic (have at
>> least one virtual function).

>
> Wouldn't that be: "have a vritual destructor"?


No. He is right.
 
Reply With Quote
 
Fernando Gˇmez
Guest
Posts: n/a
 
      05-28-2008
On May 28, 2:15 pm, David C˘me <(E-Mail Removed)> wrote:
> On Wed, 28 May 2008 21:07:26 +0200, Fernando Gˇmez
>
>
>
> <(E-Mail Removed)> wrote:
> > On May 28, 12:48 pm, Victor Bazarov <(E-Mail Removed)> wrote:
> >> (E-Mail Removed) wrote:
> >> > Suppose I have the following three classes,

>
> >> > GrandBase <-- Base <-- Child <-- GrandChild

>
> >> > The following cast expression holds true only if pBase points object
> >> > of type of ``Child'' or ``GrandChild'', i.e. types not upper than
> >> > Child in the above class hierarchy,

>
> >> > dynamic_cast<Child*>pBase

>
> >> > Do I draw this correctly?

>
> >> You missed the parentheses and your class has to be polymorphic (have at
> >> least one virtual function).

>
> > Wouldn't that be: "have a vritual destructor"?

>
> No. He is right.


So, can you have a polymorphic class without a virtual destructor?
 
Reply With Quote
 
Erik Wikstr├Âm
Guest
Posts: n/a
 
      05-28-2008
On 2008-05-28 21:07, Fernando G├│mez wrote:
> On May 28, 12:48 pm, Victor Bazarov <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>> > Suppose I have the following three classes,

>>
>> > GrandBase <-- Base <-- Child <-- GrandChild

>>
>> > The following cast expression holds true only if pBase points object
>> > of type of ``Child'' or ``GrandChild'', i.e. types not upper than
>> > Child in the above class hierarchy,

>>
>> > dynamic_cast<Child*>pBase

>>
>> > Do I draw this correctly?

>>
>> You missed the parentheses and your class has to be polymorphic (have at
>> least one virtual function).

>
> Wouldn't that be: "have a vritual destructor"?


No, any functions will do. Note that the destructor is a member function
(thought a special one).

--
Erik Wikstr├Âm
 
Reply With Quote
 
Marcel MŘller
Guest
Posts: n/a
 
      05-28-2008
(E-Mail Removed) wrote:
> Suppose I have the following three classes,
>
> GrandBase <-- Base <-- Child <-- GrandChild
>
> The following cast expression holds true only if pBase points object
> of type of ``Child'' or ``GrandChild'', i.e. types not upper than
> Child in the above class hierarchy,


In case of multiple inheritance it becomes a bit more complicated. In
fact it returns a non-null pointer to Child if and only if the cast to
Child* is valid. But it may also be a cross cast where Child is neither
derived from Base nor a superclass of it.

> dynamic_cast<Child*>pBase
>
> Do I draw this correctly?
>
> Up-casting is safe while down-casting is not. dynamic_cast seems to do
> down-casting, so why is dynamic_cast intenting to do the dangerous
> work?


dynamic_cast is the C++ way of checking whether a polymorphic object is
of a certain type at runtime. You need this e.g. for type specific
dispatching. I would not call this 'dangerous work'.


Marcel
 
Reply With Quote
 
lovecreatesbea...@gmail.com
Guest
Posts: n/a
 
      05-29-2008
On May 29, 1:46 am, Juha Nieminen <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > Up-casting is safe while down-casting is not. dynamic_cast seems to do
> > down-casting, so why is dynamic_cast intenting to do the dangerous
> > work?

>
> Dynamic casting is intended to do the "dangerous work" more safely.
> While a static_cast will simply cast to the derived type regardless of
> whether the object is of that type,


Hi, thank you.

dynamic_cast<T>(pointer) is used with an object pointer, while
static_cast<T>(object) is the implicitly compatible conversion and
works with objects also. Am I right?

> dynamic cast actually checks that
> the object is of that type, and if it isn't, it returns a null pointer.
> You can check the returned pointer to see if the cast was successful.
> (With static_cast you probably just get a segmentation fault.)


``is of that type'' means that

dynamic_cast<T>(pointer);

(*pointer) is declared as T or T's derived classes, right?

This cast fails if (*pointer) is declared as T's parent classes or
other un-related classes. Am I right?

> There are situation where downcasting may be unavoidable or the path
> of least resistance. Thus it's good to have a tool to make it safer.


I read some past posts, some said dynamic_cast indicated flaw in
design
 
Reply With Quote
 
lovecreatesbea...@gmail.com
Guest
Posts: n/a
 
      05-29-2008
On May 29, 1:48 am, Victor Bazarov <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > Suppose I have the following three classes,


That should be four , I added GrandBase but forgot to change the
number.

>
> > GrandBase <-- Base <-- Child <-- GrandChild

>
> > The following cast expression holds true only if pBase points object
> > of type of ``Child'' or ``GrandChild'', i.e. types not upper than
> > Child in the above class hierarchy,

>
> > dynamic_cast<Child*>pBase

>
> > Do I draw this correctly?

>
> You missed the parentheses and your class has to be polymorphic (have at
> least one virtual function). That's first. And second, the expression
>
> dynamic_cast<Child*>(pBase)
>
> has the type 'Child*', and not "holds true", although it will evaluate
> to (Child*)0 if the cast fails.
>


Yes, thank you for correcting me.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-29-2008
On May 29, 3:49 am, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> On May 29, 1:46 am, Juha Nieminen <(E-Mail Removed)> wrote:


> > (E-Mail Removed) wrote:
> > > Up-casting is safe while down-casting is not. dynamic_cast
> > > seems to do down-casting, so why is dynamic_cast intenting
> > > to do the dangerous work?


> > Dynamic casting is intended to do the "dangerous work" more
> > safely. While a static_cast will simply cast to the derived
> > type regardless of whether the object is of that type,


> dynamic_cast<T>(pointer) is used with an object pointer, while
> static_cast<T>(object) is the implicitly compatible conversion
> and works with objects also. Am I right?


Both dynamic_cast and static_cast can be used on pointers and
references. (static_cast can also be used in a lot of other
cases.) When used on pointers or references:

-- static_cast allows moving up or down in the hierarchy (but
not sideways); it also allows converting to or from void*.
When moving down (Base* to Derived*), it makes no
verifications; if the object actually pointed to isn't a
Derived, anything can happen, so it is extremely dangerous
for this sort of casting. Note too that there are some
restrictions on static_cast when virtual base classes are
involved.

-- dynamic_cast only makes sense on pointers or references to
polymorphic types---otherwise, it only allows casting up
(Derived* to Base*), where it has exactly the same semantics
as static_cast. On pointers to polymorphic types, it allows
arbitrary navigation within the hierarchy, including
sideways. It verifies the type: if you dynamic_cast to
Derived*, and the pointer doesn't actually point to a
Derived*, it returns a null pointer (or raises an exception
if it is a reference cast). Except in very exceptional
cases (when the profiler says you must), you should use it
exclusively for down casting.

> > dynamic cast actually checks that the object is of that
> > type, and if it isn't, it returns a null pointer. You can
> > check the returned pointer to see if the cast was
> > successful. (With static_cast you probably just get a
> > segmentation fault.)


> ``is of that type'' means that


> dynamic_cast<T>(pointer);


> (*pointer) is declared as T or T's derived classes, right?


> This cast fails if (*pointer) is declared as T's parent
> classes or other un-related classes. Am I right?


I'm not too sure I understand. If pointer is of type Derived*,
dynamic_cast< Base* >( pointer ) always succeeds, of course,
returning a pointer to the Base sub-object of the Derived. If
pointer is of type Base*, dynamic_cast< Derived* >( pointer )
will return a null pointer if the actual object doesn't contain
an unambiguous Derived in its inheritance hierarchy, and a
pointer to the Derived if it does.

> > There are situation where downcasting may be unavoidable or
> > the path of least resistance. Thus it's good to have a tool
> > to make it safer.


> I read some past posts, some said dynamic_cast indicated flaw
> in design


There is no requirement here for someone to be competent before
posting. dynamic_cast is easily abused, but there are cases
where it corresponds to the best design, and there are cases
where it represents the best engineering compromize.

Having a string of dynamic_cast usually is a sign of poor
design.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientÚe objet/
Beratung in objektorientierter Datenverarbeitung
9 place SÚmard, 78210 St.-Cyr-l'╔cole, France, +33 (0)1 30 23 00 34

 
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
typeid and dynamic_cast, gcc 3.3 Andreas Sch. C++ 18 01-29-2004 10:24 PM
typeid() faster than dynamic_cast<> Jamie Burns C++ 11 01-29-2004 08:54 PM
how static_cast and dynamic_cast implemented? Yuming Ma C++ 1 12-17-2003 12:58 AM
dynamic_cast and references Dan Noland C++ 0 07-29-2003 09:43 PM
dynamic_cast<> alg C++ 3 07-14-2003 09:08 AM



Advertisments