dynamic cast

V

vsgdp

Must a class have virtual functions to use dynamic cast?

If so, and I need to still downcast between a small hierarchy in which there
are no methods, I should use static_cast ?
 
N

Nick Hounsome

vsgdp said:
Must a class have virtual functions to use dynamic cast?
yes
If so, and I need to still downcast between a small hierarchy in which there
are no methods, I should use static_cast ?
yes
 
A

Andrey Tarasevich

vsgdp said:
Must a class have virtual functions to use dynamic cast?

It depends on how you use 'dynamic_cast'. For downcasts and cross-casts
- yes, a class must be polymorphic, i.e. it must contain at least one
virtual function. For upcasts this is not required.
If so, and I need to still downcast between a small hierarchy in which there
are no methods, I should use static_cast ?

You can use 'static_cast' in this case, but you won't get any run-time
checking capabilities of 'dynamic_cast'. Decide whether it is really
important in your case.
 

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
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top