dynamic_pointer_cast Function

对情况的动态转换。

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 指针;否则它返回拥有资源。 sp拥有的 shared_ptr Class<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); 
    } 
 
  

要求

**标题:**memory

命名空间: std

请参见

参考

shared_ptr Class

const_pointer_cast Function

static_pointer_cast Function

其他资源

memory 成员