例如,下面的代码使用
dynamic_pointer_cast<T>()把一个shared_ptr<std::exception>向下转型为一个
shared_ptr<std::exception>,然后又用
static_pointer_cast<T>()重新转为
shared_ptr<std::exception>:
- shared_ptr<std::exception> sp1(new bad_exception("error"));
- shared_ptr<bad_exception> sp2 = dynamic_pointer_cast<bad_exception>(sp1);
- shared_ptr<std::exception> sp3 = static_pointer_cast<std::exception>(sp2);
- assert(sp3 == sp1);