On Sat, 2012-06-16, Tobias Müller wrote:
> Jorgen Grahn <grahn+> wrote:
>> On Fri, 2012-06-15, Tobias Müller wrote:
>>> Juha Nieminen <> wrote:
>>>> wrote:
>>>>> unique_ptr<Movie> p(new Movie(...));
>>>>> movies.push_back(p.get());
>>>>> p.release();
>>>>>
>>>>> Movie* p = new Movie(...);
>>>>> try { movies.push_back(p); }
>>>>> catch(...)
>>>>> {
>>>>> delete p;
>>>>> throw;
>>>>> }
>>>>>
>>>>> std::vector< std::shared_ptr<Movie> >;
>>>>
>>>> What is wrong with just std::vector<Movie>?
>>>
>>> It is probably just a an example of a Problem that happened in a larger
>>> context.
>>> 1. When Movie is a large object (e.g. contains the cover image) and has no
>>> move constructor, copying is expensive.
>>> 2. There may other objects containing pointers to that movies. This is at
>>> least problematic. You have to be sure, that the objects are never copied.
>>> 3. If there are subclasses of Movie you have to use pointers.
>>
>> Yes -- except the OP's very basic questions makes one wonder if one of
>> those was his reason, or if the vector<T*> design happened by accident
>> or misunderstanding.
>>
>> /Jorgen
>
> Even if that's the case, it leads him to the wrong direction.
No, the default strategy for everyone should be not to mess with
pointers, plain or smart. It's things like 1--3 above which may force
you to do it.
> A std::vector<std::shared_ptr<Movie>> almost equally safe with respect to
> memory leaks.
> The problems you can run into with a std::vector<Movie> however can be much
> more subtle and difficult to even notice them. Consider an insert or a
> push_front into the vector. In the most cases all pointers to the existing
> objects are still valid but now pointing to different objects.
But everyone has to learn to handle that anyway! Unless they /never/
store objects by value in containers, and that would look rather
unusual.
(Of course, this being comp.lang.c++, someone will now claim
containers of pointers is the only way to do it, and be very
insistent.)
/Jorgen
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .