다음을 통해 공유


dynamic_pointer_cast

Dynamic cast to shared_ptr.

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

매개 변수

  • Ty
    The type controlled by the returned shared pointer.

  • Other
    The type controlled by the argument shared pointer.

  • sp
    The argument shared pointer.

설명

The template function returns an empty shared_ptr object if dynamic_cast<Ty*>(sp.get()) returns a null pointer; otherwise it returns a shared_ptr 클래스<Ty> object that owns the resource that is owned by sp. The expression dynamic_cast<Ty*>(sp.get()) must be valid.

예제

 

// 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 클래스

const_pointer_cast

static_pointer_cast

기타 리소스

<memory> 멤버