dynamic cast questions

P

puzzlecracker

What about dynamic_cast when dynamic reference is not unique (e.g. A-
B, B->C, B->D, is dynamic_cast<B *> correct for pointer to D?

Why would you use dynamic cast<void *>?

What is typeid & what does it return? How would you use it?

What is wrong here

void *pVoid = new C(); C *pC = dynamic_cast<C *> (pVoid);
 
P

puzzlecracker

Where have you actually seen any of these constructs? This smells like
homework.

lazy to google it, I found these guys at c++ trivia forum, I actually
have lots more of this intricate questions.

Not really in the need of solutions, just wanted you guys to see c++
from the unknown angle...
 
J

James Kanze

What about dynamic_cast when dynamic reference is not unique
(e.g. A-> B, B->C, B->D, is dynamic_cast<B *> correct for
pointer to D?

Obviously. So is static_cast, and any time static_cast is OK,
so is dynamic_cast.

But I don't understand the comments above. B is unique.
Perhaps you meant something like:

B B
| |
L R
\ /
D

D* pD = new D ;
B* pB = dynamic_cast< B* >( pD ) ;

Most people would probably try static_cast in such cases, but
the results are the same, static_cast or dynamic_cast: the
conversion is ambiguous, and thus rejected by the compiler.

Perhaps a more complete example of what you're asking would be
in order. Or just read the exact rules (§5.2.7/8 in the
standard).
Why would you use dynamic cast<void *>?

Because you want the void* to point to the complete object, and
not just to the sub-object pointed to by the pointer you have.
What is typeid

An operator.
& what does it return?

Since it's not a function, it doesn't return anything. The
result of the operator is an lvalue of type std::type_info
const, or of some implementation defined type derived from
std::type_info (also const).
How would you use it?

Depends on what you want to use it for.
What is wrong here
void *pVoid = new C(); C *pC = dynamic_cast<C *> (pVoid);

It's not legal C++. You can't dynamic_cast a void*.
 
J

James Kanze

Why don't you try google for it? Most of the time
that's a pretty fast way to get information and
answers.

So is asking questions here. With the advantage that 1) you
don't have to figure out how to enter your request (what
keywords should he google for here), and 2) there's actually a
reasonable chance that you get the right answer, and can
recognize it as such.
 
H

Hendrik Schober

James said:
So is asking questions here. With the advantage that 1) you
don't have to figure out how to enter your request (what
keywords should he google for here), and 2) there's actually a
reasonable chance that you get the right answer, and can
recognize it as such.

3) You might learn much more than just the answer to what you asked.

4) So might we.

Schobi
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top