What the hell is dynamic_cast for?

D

Daniel Pitts

James said:
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?
 
K

Kai-Uwe Bux

Daniel said:
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
 
J

James Kanze

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

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top