Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I've been making some objects using the pimpl idiom, but I'm not sure whether to used <code>shared_ptr</code> or <code>unique_ptr</code>.</p> </blockquote> <p>Definitely <code>unique_ptr</code> or <code>scoped_ptr</code>.</p> <p><code>Pimpl</code> is not a pattern, but an idiom, which deals with compile-time dependency and binary compatibility. It should not affect the semantics of the objects, especially with regard to its copying behavior.</p> <p>You may use whatever kind of smart pointer you want under the hood, but those 2 guarantee that you won't accidentally share the implementation between two distinct objects, as they require a conscious decision about the implementation of the copy constructor and assignment operator.</p> <blockquote> <p>However, these objects in a way really aren't being copied, as changes affect all copies, so I was wondering that perhaps using <code>shared_ptr</code> and allowing copies is some sort of anti-pattern or bad thing.</p> </blockquote> <p>It is not an anti-pattern, in fact, it is a pattern: Aliasing. You already use it, in C++, with bare pointers and references. <code>shared_ptr</code> offer an extra measure of "safety" to avoid dead references, at the cost of extra complexity and new issues (beware of cycles which create memory leaks).</p> <hr> <p><em>Unrelated to Pimpl</em></p> <blockquote> <p>I understand <code>unique_ptr</code> is more efficient, but this isn't so much of an issue for me, as these objects are relatively heavyweight anyway so the cost of <code>shared_ptr</code> over <code>unique_ptr</code> is relatively minor.</p> </blockquote> <p>If you can factor out some state, you may want to take a look at the <a href="http://www.vincehuston.org/dp/flyweight.html">Flyweight</a> pattern.</p>
    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