-2

現在それによって指されているメモリを解放せずに、共有ポインタが別のメモリ位置を指すようにする方法はありますか

コードを検討してください:

#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <iostream>

int
main()
{
    int *p = new int();
    *p = 10;
    int *q = new int();
    *q = 20;

    boost::shared_ptr<int> ps(p);

    // This leads to a compiler error
    ps = boost::make_shared<int>(q);

    std::cout << *p << std::endl;
    std::cout << *q << std::endl;
    return 0;
}
4

1 に答える 1

1

できません。

もちろん、デリーターをノーオペレーションに変更しながら、リリースして再アタッチすることもできます

正直なところ、あなたはただ欲しがっているように見えます

ps = boost::make_shared<int>(*q);

プリント ( Coliru でライブ):

0
20
于 2015-06-25T10:49:35.460 に答える