dynamic_pointer_cast
为的情况下动态的转换。
template <class Ty, class Other>
shared_ptr<Ty> dynamic_pointer_cast(const shared_ptr<Other>& sp);
参数
Ty
返回的共享指针控制的类型。Other
参数控制的类型共享指针。sp
共享指针参数。
备注
模板函数的情况下返回空 dynamic_cast<Ty*>(sp.get()) 对象是否返回 null 指针;否则返回的资源由 spshared_ptr 类拥有的<Ty> 对象。 dynamic_cast<Ty*>(sp.get()) 绑定表达式是有效的。
示例
// std_tr1__memory__dynamic_pointer_cast.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
struct base
{
virtual ~base()
{
}
int val;
};
struct derived
: public base
{
};
int main()
{
std::shared_ptr<base> sp0(new derived);
std::shared_ptr<derived> sp1 =
std::dynamic_pointer_cast<derived>(sp0);
sp0->val = 3;
std::cout << "sp1->val == " << sp1->val << std::endl;
return (0);
}
要求
页眉: <内存>
命名空间: std