# Adding an object containing a ScopedPointer to a collection

**URL:** <https://forum.juce.com/t/adding-an-object-containing-a-scopedpointer-to-a-collection/11910>\
**Category:** General JUCE discussion\
**Created:** [November 21, 2013, 4:34pm UTC](https://forum.juce.com/t/adding-an-object-containing-a-scopedpointer-to-a-collection/11910 "2013-11-21T16:34:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![CowTipper](https://avatars.discourse-cdn.com/v4/letter/c/7c8e57/32.png) [@CowTipper](https://forum.juce.com/u/CowTipper)\
**Post date:** [November 21, 2013, 4:34pm UTC](https://forum.juce.com/t/adding-an-object-containing-a-scopedpointer-to-a-collection/11910/1 "2013-11-21T16:34:35Z")

</div>

Here's a contrived example:

```
// Does not compile - ListItem has no copy constructor available
struct ListItem
{
    ListItem(File* initialFile) : file(initialFile) { }

    ScopedPointer<File> file;
};

Array<ListItem> List;

List.add(ListItem(nullptr));
```

This won't compile because List.add() needs to take a copy of the temporary ListItem in order to do its in-place new.&nbsp; Now, scoped pointers do allow copies (by transferring ownership of their pointer from the source to the dest object), but&nbsp;since&nbsp;the temporary copy is passed to List.add() as a const reference, it can't modify the source object to transfer the ownership.

Is this a good place to use OwnedArray in place of Array, and pass in a heap allocated&nbsp;ListItem?

---

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [November 21, 2013, 11:07pm UTC](https://forum.juce.com/t/adding-an-object-containing-a-scopedpointer-to-a-collection/11910/2 "2013-11-21T23:07:37Z")

</div>

Why not just use std::vector and std::unique\_ptr?

&nbsp;
