Is it safe to store class objects with std :: auto_ptr as its member variable in std :: vector?

I can not use shared_ptr in my project, without boost :(

So, I have a class that looks something like the one below:

class MyClass
{
private:
  std::auto_ptr<MyOtherClass> obj;
};

Now I want to save instances of the above class in std :: vector. It is safe? I read here that it is wrong to use std :: auto_ptr with STL containers. Does this apply to my situation here?

+3
source share
8 answers

I posted the question as a follow-up to this answer; see Class containing auto_ptr stored in vector .

, , , , (. ) . ( , ), auto_ptr. transanferring , , , , .

, , , auto_ptr . , , :

, - . , , .

  • , , , boost
+2

, , MyClass instnace, - auto_ptr , ​​ , ( auto_ptr )

BTW:

private:
  MyClass& operator=( const MyClass& );  
  MyClass( const MyClass& );

, , .

+6

, auto_ptr, , .

boost:: shared_ptr , , boost.

, boost, , shared\ptr, bcp tool boost:: shared_ptr. hpp . , .

+3

, auto_ptr . undefined. :

  • std::vector < > :: resize . "" (. ), , !
  • - , , ( ), - , push_back , . , .

undefined, . , , , , , . -, push_back, resize .., const, T. , T const& . / T(T&) - ! , .

++ . , . . , .

, (17.4.3.6):

( , , , ) ++ , ++. , .

, undefined :

  • , , (20.1.5, 23.1, 24.1, 26.1).
+3

MyClass . auto_ptr < > , ( ) auto_ptr < > . , , .

+1

, auto_pointer, , : sort(), . (sort() quicksort, "" ) , , scpope sort(). , , .

, , auto_ptr, , ​​/, . , :)

, -.

- boost auto_ptr, /.

, , / : ( ), sort().

+1

, auto_ptr , - :

class MyClass
{
private:
  const std::auto_ptr<MyOtherClass> obj; // Note const here to keep the pointer from being modified.

public:
  MyClass(const MyClass &other) : obj(new MyOtherClass(*other.obj)) {}
  MyClass &operator=(const MyClass &other)
  {
      *obj = *other.obj;
      return *this;
  }
};

, , , , auto_ptr. , , auto_ptr. auto_ptrs, , . , .

+1

. auto_ptr , , .

boost:: shared_ptr.

0

Source: https://habr.com/ru/post/1705635/


All Articles