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?

 
 
Daniel Pitts
Guest
Posts: n/a
 
      05-30-2008
James Kanze wrote:
> On May 30, 4:05 am, "(E-Mail Removed)"
> <(E-Mail Removed)> wrote:
>> ``C++ Primer, 4th'' says in $18.2.1, ``If a dynamic_cast to a
>> pointer type fails, the result of the dynamic_cast is the
>> value 0.'' If the dynamic_cast expression evaluates zero, can
>> I regard it as failed?

>
> Yes. Don't confuse legal/illegal with succeeded/failed. An
> illegal dynamic_cast won't compile. But a legal dynamic_cast
> can fail at runtime, in which case, it either returns a null
> pointer (pointer cast) or throws an exception (reference cast).
>

Just for sake of completeness, what does dynamic_cast do with an already
null pointer?

p = 0;
pD = dynamic_cast<Derived *>(p); // exception, sigsegv, or null?

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      05-30-2008
Daniel Pitts wrote:

> James Kanze wrote:
>> On May 30, 4:05 am, "(E-Mail Removed)"
>> <(E-Mail Removed)> wrote:
>>> ``C++ Primer, 4th'' says in $18.2.1, ``If a dynamic_cast to a
>>> pointer type fails, the result of the dynamic_cast is the
>>> value 0.'' If the dynamic_cast expression evaluates zero, can
>>> I regard it as failed?

>>
>> Yes. Don't confuse legal/illegal with succeeded/failed. An
>> illegal dynamic_cast won't compile. But a legal dynamic_cast
>> can fail at runtime, in which case, it either returns a null
>> pointer (pointer cast) or throws an exception (reference cast).
>>

> Just for sake of completeness, what does dynamic_cast do with an already
> null pointer?
>
> p = 0;
> pD = dynamic_cast<Derived *>(p); // exception, sigsegv, or null?


If the argument to dynamic_cast<T*> is 0, the cast evaluates to 0. See
[5.2.7/4].


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      05-31-2008
On May 30, 6:48 pm, Daniel Pitts
<(E-Mail Removed)> wrote:
> James Kanze wrote:
> > On May 30, 4:05 am, "(E-Mail Removed)"
> > <(E-Mail Removed)> wrote:
> >> ``C++ Primer, 4th'' says in $18.2.1, ``If a dynamic_cast to a
> >> pointer type fails, the result of the dynamic_cast is the
> >> value 0.'' If the dynamic_cast expression evaluates zero, can
> >> I regard it as failed?


> > Yes. Don't confuse legal/illegal with succeeded/failed. An
> > illegal dynamic_cast won't compile. But a legal dynamic_cast
> > can fail at runtime, in which case, it either returns a null
> > pointer (pointer cast) or throws an exception (reference cast).


> Just for sake of completeness, what does dynamic_cast do with
> an already null pointer?


> p = 0;
> pD = dynamic_cast<Derived *>(p); // exception, sigsegv, or null?


It's guaranteed to return a null pointer. This is true for all
pointer casts, in fact. (Which means that even static_cast may
require some actual code to be executed.)

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