# Using OwnedArray methods inside Listener

**URL:** <https://forum.juce.com/t/using-ownedarray-methods-inside-listener/19468>\
**Category:** General JUCE discussion\
**Created:** [October 13, 2016, 6:53pm UTC](https://forum.juce.com/t/using-ownedarray-methods-inside-listener/19468 "2016-10-13T18:53:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![swdpowell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/swdpowell/32/13_2.png) [@swdpowell](https://forum.juce.com/u/swdpowell)\
**Post date:** [October 13, 2016, 6:53pm UTC](https://forum.juce.com/t/using-ownedarray-methods-inside-listener/19468/1 "2016-10-13T18:53:38Z")

</div>

Hello.

I want to be able to do this:

void buttonClicked (Button\* b)  
{  
if (ownedArray.contains (b)) function (ownedArray.indexOf (b));  
}

However, the OwnedArray methods require a const Button pointer. Const cast doesn’t seem to work either as you can’t cast to base type button. Is there / what’s the best way round this?

Thank you.

---

<div class="post-metadata">

**Author:** ![willrice](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/willrice/32/55_2.png) [@willrice](https://forum.juce.com/u/willrice)\
**Post date:** [October 13, 2016, 6:55pm UTC](https://forum.juce.com/t/using-ownedarray-methods-inside-listener/19468/2 "2016-10-13T18:55:55Z")

</div>

```auto
void buttonClicked (Button* b)
{
    const Button* button (b);
    if (ownedArray.contains (button))
    {
        // do stuff
    }
}

```

?

---

<div class="post-metadata">

**Author:** ![willrice](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/willrice/32/55_2.png) [@willrice](https://forum.juce.com/u/willrice)\
**Post date:** [October 13, 2016, 8:32pm UTC](https://forum.juce.com/t/using-ownedarray-methods-inside-listener/19468/3 "2016-10-13T20:32:18Z")

</div>

Turns out that even that’s not necessary. See [my rough ‘n’ ready example](https://github.com/willricemusic/OwnedArrayOfButtons/blob/master/Source/MainComponent.cpp#L57)…

![](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/f/f594554c2ad24a2ecb8bd304f6eb9d90dee98b48.gif)

Guess there’s something else wrong…? If your `buttonClicked` definition is in a .cpp, perhaps you forgot to specify the scope of the method? Or if it’s in a .h, perhaps you forgot the `override` keyword?

Which type have you passed as the template parameter of your OwnedArray\<\> ?

---

<div class="post-metadata">

**Author:** ![swdpowell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/swdpowell/32/13_2.png) [@swdpowell](https://forum.juce.com/u/swdpowell)\
**Post date:** [October 14, 2016, 8:00am UTC](https://forum.juce.com/t/using-ownedarray-methods-inside-listener/19468/4 "2016-10-14T08:00:08Z")

</div>

Right, with your help I worked out the problem.

At first, your solutions didn’t work. I’ve found that this was because my OwnedArray was (understandably?) set as OwnedArray , and altering it to OwnedArray solved the problem. Also it doesn’t seem necessary to convert between const and not. I’d never initialised an array of base with a derived object.

Thank you!

---

<div class="post-metadata">

**Author:** ![willrice](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/willrice/32/55_2.png) [@willrice](https://forum.juce.com/u/willrice)\
**Post date:** [October 14, 2016, 9:57am UTC](https://forum.juce.com/t/using-ownedarray-methods-inside-listener/19468/5 "2016-10-14T09:57:48Z")

</div>

> [@swdpowell](#):
>
> I’ve found that this was because my OwnedArray was (understandably?) set as OwnedArray , and altering it to OwnedArray solved the problem.

![](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/5/5af465f3f483c92b40f33e7a161330c4adde90f9.gif)

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [October 14, 2016, 2:28pm UTC](https://forum.juce.com/t/using-ownedarray-methods-inside-listener/19468/6 "2016-10-14T14:28:52Z")

</div>

😂 looks like it went through a little too much “find & replace”

---

<div class="post-metadata">

**Author:** ![willrice](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/willrice/32/55_2.png) [@willrice](https://forum.juce.com/u/willrice)\
**Post date:** [October 14, 2016, 4:02pm UTC](https://forum.juce.com/t/using-ownedarray-methods-inside-listener/19468/7 "2016-10-14T16:02:02Z")

</div>

In seriousness, **can others please confirm / refute** that it’s OK to do:

```auto
OwnedArray<BaseType> things;
things.add (new DerivedType());

for (int i = 0; i < things.size(); ++i)
{
    things[i]->baseTypeMethod();
    static_cast<DerivedType*>(things[i])->derivedTypeMethod();
}

```

which is the crux of my suggested solution…

---

<div class="post-metadata">

**Author:** ![jonathonracz](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@jonathonracz](https://forum.juce.com/u/jonathonracz)\
**Post date:** [October 14, 2016, 4:24pm UTC](https://forum.juce.com/t/using-ownedarray-methods-inside-listener/19468/8 "2016-10-14T16:24:54Z")

</div>

The behavior is undefined if `things` contains _any_ objects which are not actually of type `DerivedType` when you call the derived method using the `static_cast`. So this would really only be safe if you were positive that `things` _only_ contained objects of `DerivedType`… however in that case you would just make `things` an `OwnedArray<DerivedType>`.

This situation is exactly what `dynamic_cast` was created to handle:

```
OwnedArray<BaseType> things;
things.add (new DerivedType());

for (int i = 0; i < things.size(); ++i)
{
    things[i]->baseTypeMethod();

    DerivedType* castedType = dynamic_cast<DervicedType*>(things[i]);
    if (castedType != nullptr)
        castedType->derivedTypeMethod();
}
```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [October 14, 2016, 9:17pm UTC](https://forum.juce.com/t/using-ownedarray-methods-inside-listener/19468/9 "2016-10-14T21:17:03Z")

</div>

+1

But _please_ everyone get into the habit of writing it like this:

```
if (auto castedType = dynamic_cast<DervicedType*>(things[i]))
    castedType->derivedTypeMethod();

```

It’s shorter and safer that way.
