static_pointer_cast Function

Static cast to shared_ptr.

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

Parameters

  • Ty
    The type controlled by the returned shared pointer.

  • Other
    The type controlled by the argument shared pointer.

  • Other
    The argument shared pointer.

Remarks

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

Example

 

// 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::tr1::shared_ptr<base> sp0(new derived); 
    std::tr1::shared_ptr<derived> sp1 = 
        std::tr1::static_pointer_cast<derived>(sp0); 
 
    sp0->val = 3; 
    std::cout << "sp1->val == " << sp1->val << std::endl; 
 
    return (0); 
    } 
 

sp1->val == 3

Requirements

Header: <memory>

Namespace: std::tr1

See Also

Reference

<memory> (TR1)

shared_ptr Class

const_pointer_cast Function

dynamic_pointer_cast Function