Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>C++98 does not provide any smart pointers except <code>auto_ptr</code> which is fraught with its own issues. C++0X tries to fix this by bringing in a few more varieties (<code>shared_ptr</code>, <code>unique_ptr</code> etc.). In the meantime the best bet is to use Boost. Take a look at the various flavors available to you <a href="http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/smart_ptr.htm" rel="noreferrer">here</a>. Boost is community driven, extensively tested and of course free. There is excellent documentation with sample code that will help you get started.</p> <blockquote> <p>Can you explain what are the types of smart pointers, how do they work and when to use them?</p> </blockquote> <p>There are a number of them. In short:</p> <blockquote> <p><strong><code>scoped_ptr</code></strong> <code>&lt;boost/scoped_ptr.hpp&gt;</code> Simple sole ownership of single objects. Noncopyable.</p> <p><strong><code>scoped_array</code></strong> <code>&lt;boost/scoped_array.hpp&gt;</code> Simple sole ownership of arrays. Noncopyable.</p> <p><strong><code>shared_ptr</code></strong> <code>&lt;boost/shared_ptr.hpp&gt;</code> Object ownership shared among multiple pointers. </p> <p><strong><code>shared_array</code></strong> <code>&lt;boost/shared_array.hpp&gt;</code> Array ownership shared among multiple pointers. </p> <p><strong><code>weak_ptr</code></strong> <code>&lt;boost/weak_ptr.hpp&gt;</code> Non-owning observers of an object owned by shared_ptr. </p> <p><strong><code>intrusive_ptr</code></strong> <code>&lt;boost/intrusive_ptr.hpp&gt;</code> Shared ownership of objects with an embedded reference count.</p> </blockquote> <p>(That is from Boost documentation and note that they have containers for such pointers too!)</p> <blockquote> <p>Also, what is the "protocol" when receiving or passing raw pointers in interfaces written by other people?</p> </blockquote> <p>For me the most important rules are:</p> <ul> <li>Const-qualification</li> <li>Not to deallocate stuff I did not allocate</li> <li>Check for transfer of ownership/move semantics </li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload