次の方法で共有


const_pointer_cast 関数

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::shared_ptr<int> sp0(new int); 
    std::shared_ptr<const int> sp1 = 
        std::const_pointer_cast<const int>(sp0); 
 
    *sp0 = 3; 
    std::cout << "sp1 == " << *sp1 << std::endl; 
 
    return (0); 
    } 
 
  

必要条件

ヘッダー : <memory>

名前空間: std

参照

関連項目

shared_ptr クラス

dynamic_pointer_cast 関数

static_pointer_cast 関数