static_pointer_cast Function

静态转换为的情况。

template <class Ty, class Other>
    shared_ptr<Ty> static_pointer_cast(const shared_ptr<Other>& sp);

参数

  • Ty
    返回的共享指针控件的类型。

  • Other
    参数控制的类型共享指针。

  • Other
    参数共享指针。

备注

模板函数返回空的情况对象 sp 是否为空 shared_ptr 对象;否则它返回拥有资源。sp拥有的 shared_ptr Class<Ty;AMP_gt; 对象。该表达式 static_cast<Ty*;AMP_gt;(sp.get()) 必须是有效的。

示例

 

// std_tr1__memory__static_pointer_cast.cpp 
// compile with: /EHsc 
#include <memory> 
#include <iostream> 
 
struct base 
    { 
    int val; 
    }; 
 
struct derived 
    : public base 
    { 
    }; 
 
int main() 
    { 
    std::shared_ptr<base> sp0(new derived); 
    std::shared_ptr<derived> sp1 = 
        std::static_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

dynamic_pointer_cast Function