次の方法で共有


static_pointer_cast 関数

更新 : 2007 年 11 月

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::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

必要条件

ヘッダー : <memory>

名前空間 : std::tr1

参照

参照

<memory> (TR1)

shared_ptr クラス

const_pointer_cast 関数

dynamic_pointer_cast 関数