c++ - Why an auto_ptr can "seal" a container -


said that "an auto-IPT containing an STL container can be used to prevent further modification of the container. . " The following example was used:

  auto_ptr  Push_back (8); / />  

If I decrease the last line, then G ++ will report an error

  t05.cpp: 24: error : Const std :: vector & lt; Int, std :: allocation & lt; Int & gt; & Gt; 'Zero' std :: vector as & lt; _Tp, _Alloc & gt; : [_Tp = int, _Alloc = std :: allocator & lt; as the 'this' argument of push_back (const _Tp & amp; Int & gt;] with 'qualifier discards  

Why am I anxious after transferring ownership of this vector, can not it be modified now?

Thanks a lot!

indicator const vector < ContainedType & gt; The type close_vec indicator keeps. Because this type is const , you can not call it any method, which is also not defined as const (which means that it is internal The data do not change). Naturally, push_back is non-constant, because this vector changes, so you can not call it at a pointpoint it actually does something to do with auto_ptr Not even this, you can also complete it with regular pointers:

  vector & lt; ContainedType & gt; * Open_vec = new vector & lt; ContainedType & gt; (); Open_vec- & gt; Push_back (5); Open_vec- & gt; Push_back (3); Const Venctor & lt; ContainedType & gt; * Close_vec = open_vec; Close_vec-> Push_back (8); // failed  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -