# Best practice passing PropertiesFile pointer around Components

**URL:** <https://forum.juce.com/t/best-practice-passing-propertiesfile-pointer-around-components/13974>\
**Category:** General JUCE discussion\
**Created:** [December 4, 2014, 11:19am UTC](https://forum.juce.com/t/best-practice-passing-propertiesfile-pointer-around-components/13974 "2014-12-04T11:19:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adamwilson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/adamwilson/32/1387_2.png) [@adamwilson](https://forum.juce.com/u/adamwilson)\
**Post date:** [December 4, 2014, 11:19am UTC](https://forum.juce.com/t/best-practice-passing-propertiesfile-pointer-around-components/13974/1 "2014-12-04T11:19:53Z")

</div>

I've just started using the PropertiesFile class in my app, and want to share settings among components.&nbsp;

I am thinking of warpping it in a class that inherits ReferenceCountedObject so can safely pass a pointer around.

I'm planning to initialise it in MainContentComponent and then pass the pointer&nbsp;along as a constructor argument&nbsp;when subcomponents are created. This is how I'm currently sharing other objects across components.&nbsp;

Is this good practice or is there a more suitable method?&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![otristan](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@otristan](https://forum.juce.com/u/otristan)\
**Post date:** [December 4, 2014, 1:00pm UTC](https://forum.juce.com/t/best-practice-passing-propertiesfile-pointer-around-components/13974/2 "2014-12-04T13:00:27Z")

</div>

if you don't mind c++11, my advice is to use std::shared\_ptr

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [December 4, 2014, 1:05pm UTC](https://forum.juce.com/t/best-practice-passing-propertiesfile-pointer-around-components/13974/3 "2014-12-04T13:05:19Z")

</div>

Personally, for a singleton type object like a PropertiesFile I would use a SharedResourcePointer. Just keep one alive in your JUCEApplication subclass and then create one whenever you need it. This saves storing and passing reference counted pointers around.&nbsp;

---

<div class="post-metadata">

**Author:** ![adamwilson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/adamwilson/32/1387_2.png) [@adamwilson](https://forum.juce.com/u/adamwilson)\
**Post date:** [December 4, 2014, 2:41pm UTC](https://forum.juce.com/t/best-practice-passing-propertiesfile-pointer-around-components/13974/4 "2014-12-04T14:41:43Z")

</div>

Thanks guys will check out the shared pointers... dont mind C++11 at all its got some nice neat stuff !

---

<div class="post-metadata">

**Author:** ![adamwilson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/adamwilson/32/1387_2.png) [@adamwilson](https://forum.juce.com/u/adamwilson)\
**Post date:** [December 4, 2014, 6:16pm UTC](https://forum.juce.com/t/best-practice-passing-propertiesfile-pointer-around-components/13974/5 "2014-12-04T18:16:28Z")

</div>

For anyone else wanting to do this, I've just found the code in Introjucer that allows Components and other classes to reference the main app class:

&nbsp;

```
class MyApp : public JUCEApplication

{

...

    static MyApp& getApp()

    {

        MyApp* const app = dynamic_cast<MyApp*> (JUCEApplication::getInstance());

        jassert (app != nullptr);

        return *app;

    }

...

}
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![adamwilson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/adamwilson/32/1387_2.png) [@adamwilson](https://forum.juce.com/u/adamwilson)\
**Post date:** [December 4, 2014, 8:36pm UTC](https://forum.juce.com/t/best-practice-passing-propertiesfile-pointer-around-components/13974/6 "2014-12-04T20:36:29Z")

</div>

I also reorganised the code like the Introjucer, putting the contents of Main.cpp into Application.h, apart from the last line which is then the only thing in Main.cpp:

```
#include "Application.h"

START_JUCE_APPLICATION (MyApp)
```

This allows including the header in other classes so they can access the shared pointers in the JUCEApplication instance.&nbsp;

I think it would be good to have a project template like this as I guess its a common pattern... as soon as you want multiple views/components&nbsp;sharing data, having everything going on in MainContentComponent doesnt make sense..&nbsp;
