Where must I instantiate a Class?


#1

Hi all,

Sometimes, I have had this problem: Where must I Instantiate an object member of a Component? Let’s see an example to focus the problem:

myComponent.h:

[code]class ContentComponent : public Component,
public MenuBarModel,
public ApplicationCommandTarget
{
public:
//==============================================================================
ContentComponent ();
~ContentComponent();

//==============================================================================
//[UserMethods]     -- You can add your own custom methods in this section.

...

//[/UserMethods]

void paint (Graphics& g);
void resized();

private:
//[UserVariables] – You can add your own custom variables in this section.

    ...

AudioDeviceManager deviceManager;
ScopedPointer<CgccArray> gccArray;
//[/UserVariables]

//==============================================================================
CTabComponent* tabbedComp;
ButtonsComponent* buttonsComp;
//==============================================================================
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ContentComponent);

};
[/code]

CgccArray ctor is:

class CgccArray : public AudioIODeviceCallback { public: CgccArray(AudioDeviceManager&); ...

Finally here is myComponent.cpp file:

[code]ContentComponent::ContentComponent ()
: tabbedComp (0),
buttonsComp (0)
{
addAndMakeVisible (tabbedComp = new CTabComponent (gccArray));
addAndMakeVisible (buttonsComp = new ButtonsComponent (gccArray));

//[UserPreSize]
deviceManager.initialise( 2, 2, 0, true, "*");
arrayGCC = new CgccArray( deviceManager );

deviceManager.addAudioCallback( gccArray );


//[/UserPreSize]

setSize (600, 400);


//[Constructor] You can add your own custom stuff here..
//[/Constructor]

[/code]

At this point, the problem is the following code:

[b][color=#800000]deviceManager.initialise( 2, 2, 0, true, "*");
arrayGCC = new CgccArray( deviceManager );[/color][/b]

must be implemented before that:

[color=#800000]addAndMakeVisible (tabbedComp = new CTabComponent (gccArray));
addAndMakeVisible (buttonsComp = new ButtonsComponent (gccArray));[/color]

Obviously, there wouldn’t be any problem in changing the order by hand. But this code is ‘custom code’ from the jucer point of view. I mean,there is no customizable space to initialize and instantiate deviceManager and arrayGcc before the subcomponents comes to life (and the subcomponents depend on gccArray object).

So my question is:

[color=#0000BF]Why there is no customizable place before subcomponents are defined?
Or better said, what am I doing wrong?
[/color]

Thank you in advance for helping me.

Gabriel


#2

You can specify initialisation list items for the class, so you could use that to instantiate member variables before anything else happens.

Or you could modify the jucer template, and add your own user-code section wherever you need to.


#3

[quote=“jules”]You can specify initialisation list items for the class, so you could use that to instantiate member variables before anything else happens.
[/quote]

But, Jules, the initialisation list does not allow to instantiate new objects, does it?


#4

You can put any expression you want in an initialiser!