Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The 'owner' of an object is not an actual part of the C++ language, but rather a conceptual tool - the idea being the 'owner' is in charge of deciding when the object can be destroyed. If an object has a single owner, it's easy to figure out when the object needs to be destroyed - just look at the owner. When an object is referenced by multiple other objects, however, things are less clear - none of the referring objects can, on its own, delete the referee object, as this would cause problems for the other referers. So there there is no "single, fixed owner".</p> <p>To give some examples, consider a 'picture' object.</p> <pre><code>class Picture { char *rawData; public: Picture(char *rawData_) : rawData(rawData_) { } Picture(const Picture &amp;p) : rawData(p.rawData) { } }; </code></pre> <p>Picture does <em>not</em> own <code>rawData</code>; as we can see from the copy constructor, it's all too easy to have multiple Pictures referencing the same data. A better version might look like:</p> <pre><code>class Picture { std::vector&lt;char&gt; rawData; public: Picture(const std::vector&lt;char&gt; &amp;rawData_) : rawData(rawData_) { } }; </code></pre> <p>This is similar, but now vector is hiding the raw pointer for us; it's impossible to have two <code>Pictures</code> reference the same pointer. And we take care of destroying the rawData array when the Picture is destroyed. In this case, the Picture owns the raw data.</p> <p>Now, you don't <em>have</em> to use STL containers to have an owned object. You could do it manually:</p> <pre><code>class Picture { size_t rawDataSize; char *rawData; public: Picture(size_t rds, char *rd) { rawDataSize = rds; rawData = new char[rds]; memcpy(rawData, rd, rds); } ~Picture() { delete [] rawData; } Picture(const Picture &amp;pic) { rawDataSize = pic.rawDataSize; rawData = new char[rawDataSize]; memcpy(rawData, pic.rawData, rawDataSize); } Picture&amp; operator=(const Picture &amp;pic) { delete [] rawData; rawDataSize = pic.rawDataSize; rawData = new char[rawDataSize]; memcpy(rawData, pic.rawData, rawDataSize); return *this; } }; </code></pre> <p>This is also an owning relationship; the rawData array 'belongs' to the picture object, permanently. All accesses go through the picture object, and it's destroyed along with the picture object.</p> <p>In general, with owning relationships, it's recommended to use wrapper classes to automate destruction, and prevent accidental copying. In my third example, had I forgotten the copy constructor or <code>operator=</code>, for example, terrible things would happen. Using wrapper classes like <code>std::unique_ptr</code>, <code>boost::scoped_ptr</code>/<code>boost::scoped_array</code>, or <code>std::vector</code> (for owned arrays) help prevent you from making mistakes.</p>
 

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