c++ - Why does the interface for auto_ptr specify two copy-constructor-like constructors -


I was going through the auto_ptr documentation on this link, something that I could not understand why it did has gone. There are two announcements for the copy creator in the interface section

1)

  auto_ptr (auto_ptr  and) throw ();  

2)

template & lt; Square Y & gt; Auto_ptr (auto_ptr & lt; y & gt; and) Throw ();

What is the purpose of this.

It is there that you can convert to points based on:

  struct base {}; Structure derived: base {}; Std :: auto_ptr & lt; Derivative & gt; D (new derivative); Std :: auto_ptr & lt; Base & gt; B (d); // converts  

In addition to this, you did not ask, but you will see that the per-constructor is non-present because that is because the owner of auto_ptr indicator will take over In the given sample, after the creation of b , d does not contain anything because it is unusable for use in containers, because auto_ptr is unusable It can not be copied.

C ++ 0x ditches auto_ptr and assign it to a unique_ptr . This indicator has the same goal, but for the right reasons, the trick-words are met. That is, while it can not be copied, it can "trick" ownership:

  std :: unique_ptr & lt; Derived & gt; D (new derivative); Std :: unique_ptr & lt; Base & gt; B (d); // is not, it can not be copied std :: unique_ptr & lt; Base & gt; B (std :: move (d)); // It can be moved  

This creates appropriate unique_ptr for use in containers, because they no longer copy their values, they Let's move.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -