dynamic_cast
时间: 2023-12-26 13:05:04 浏览: 78
dynamic_cast is an operator in C++ that is used for type casting. It is used to safely convert a pointer or reference to a class to a pointer or reference to a different class.
The syntax for dynamic_cast is:
```
dynamic_cast<new_type>(expression)
```
Here, new_type is the type to which the expression is being cast, and expression is the pointer or reference to the object being cast.
The dynamic_cast operator performs a runtime check to ensure that the cast is valid. If the cast is not valid, dynamic_cast returns a null pointer (for pointers) or throws a std::bad_cast exception (for references).
Dynamic_cast can only be used with pointers and references of polymorphic classes. A polymorphic class is a class that has at least one virtual function. If a class does not have any virtual functions, dynamic_cast cannot be used with it.
阅读全文