次の方法で共有


static_pointer_cast 関数

shared_ptr への静的なキャストを行います。

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

パラメーター

  • Ty
    返された共有ポインターによって制御される型。

  • Other
    引数の共有ポインターによって制御される型。

  • Other
    引数の共有ポインター。

解説

このテンプレート関数は、sp が空の shared_ptr オブジェクトの場合、空の shared_ptr オブジェクトを返します。それ以外の場合は、sp によって所有されたリソースを所有する shared_ptr クラス<Ty> オブジェクトを返します。式 static_cast<Ty*>(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 クラス

const_pointer_cast 関数

dynamic_pointer_cast 関数