次の方法で共有


const_pointer_cast 関数

更新 : 2007 年 11 月

shared_ptr への定数キャストを行います。

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

パラメータ

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

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

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

解説

このテンプレート関数は、const_cast<Ty*>(sp.get()) が null ポインタを返す場合は、空の shared_ptr オブジェクトを返します。それ以外の場合は、sp によって所有されているリソースを所有する shared_ptr クラス<Ty> オブジェクトを返します。式 const_cast<Ty*>(sp.get()) は有効な式であることが必要です。

使用例

 

// std_tr1__memory__const_pointer_cast.cpp 
// compile with: /EHsc 
#include <memory> 
#include <iostream> 
 
int main() 
    { 
    std::tr1::shared_ptr<int> sp0(new int); 
    std::tr1::shared_ptr<const int> sp1 = 
        std::tr1::const_pointer_cast<const int>(sp0); 
 
    *sp0 = 3; 
    std::cout << "sp1 == " << *sp1 << std::endl; 
 
    return (0); 
    } 
 
sp1 == 3

必要条件

ヘッダー : <memory>

名前空間 : std::tr1

参照

参照

<memory> (TR1)

shared_ptr クラス

dynamic_pointer_cast 関数

static_pointer_cast 関数