ウィキペディアのauto_ptrは、「STLコンテナーを含むauto_ptrを使用して、コンテナーがさらに変更されるのを防ぐことができる」と述べています。次の例を使用しました。
auto_ptr<vector<ContainedType> > open_vec(new vector<ContainedType>);
open_vec->push_back(5);
open_vec->push_back(3);
// Transfers control, but now the vector cannot be changed:
auto_ptr<const vector<ContainedType> > closed_vec(open_vec);
// closed_vec->push_back(8); // Can no longer modify
最後の行のコメントを外すと、g++は次のようにエラーを報告します
t05.cpp:24: error: passing ‘const std::vector<int, std::allocator<int> >’
as ‘this’ argument of ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&)
[with _Tp = int, _Alloc = std::allocator<int>]’ discards qualifiers
このベクターの所有権を譲渡した後、変更できなくなったのはなぜですか?
どうもありがとう!