dynamic_cast and virtual function

B

Busin

"dynamic_cast" is used for type conversion among base and derived classes.
Does it matter if base class has virtual function or not? Thanks!
 
A

Ali Cehreli

"dynamic_cast" is used for type conversion among base and derived classes.
Does it matter if base class has virtual function or not? Thanks!

The base class must have at least one virtual function for dynamic_cast to
work.

Ali
 
A

Andrey Tarasevich

Busin said:
"dynamic_cast" is used for type conversion among base and derived classes.
Does it matter if base class has virtual function or not?

It depends on the direction of conversion. There are no extra
requirements for upcasts. For downcasts and cross-casts the source type
must be polymorphic, i.e. it must have at least one virtual function.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top