Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Even though a vector copies its values, the values in your case are the pointers, and not the objects pointed to. So "<code>vm</code> has it's own copy" is not fully true: The resulting vector in your first piece of code will have a copy of the <strong>pointers</strong>, but not of the <code>MyStorageClass</code> objects they are pointing to; so, actually in all of your 3 code examples the pointers stored in <code>vm</code> would not be valid anymore if the Storage destructor is called!</p> <p>If you need to make sure, though, that <code>Storage</code> isn't destroyed before the last access to one of the MyStorageClass objects anyway, then, of the presented methods, the third way would be the preferred one, since the vector data (i.e. the pointers) is in memory only once then. You should consider returning a <code>const</code> vector, however, else every caller of getItems can modify the <code>v</code> vector of the Storage class through the returned reference.</p> <p>As others have already pointed out, exposing the vector itself might not be that good of an idea in the first place; you might consider using iterators instead or the Visitor pattern.</p> <p>Furthermore, the usage of pointers - where not absolutely needed - is in bigger projects usually rather frowned upon. Consider using smart pointers, such as <code>std::auto_ptr</code> (not very recommendable though due to strange copy semantics), or the more easily understandable <code>boost::shared_ptr</code> / <code>std::tr1::shared_ptr</code>.</p> <p>Just as a side note, the first and second code example will (at least in most modern compilers) have the exact same performance, since in the first case, the compiler can optimize the temporary return value away (check out "Return value optimization").</p>
    singulars
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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