Reputation: 47995
I've my class MIDITest
which got a member variable object called IPianoRoll
:
class MIDITest : public IPlug
{
private:
IPianoRoll mPianoRoll;
public:
MIDITest(IPlugInstanceInfo instanceInfo);
~MIDITest();
};
I need to invoke my custom CTOR of mPianoRoll
:
IPianoRoll(IPlugBase *pPlug, int x, int y, int paramIdx) : IControl(pPlug, IRECT(x, y, x + kWidth, y + kHeight), paramIdx, IChannelBlend::kBlendColorDodge) {
}
only after some other task within MIDITest
, such as:
MIDITest::MIDITest(IPlugInstanceInfo instanceInfo) : IPLUG_CTOR(kNumParams, kNumPresets, instanceInfo) {
// SOME TASKS
IGraphics *pGraphics = MakeGraphics(this, GUI_WIDTH, GUI_HEIGHT, PLUG_FPS);
pGraphics->HandleMouseOver(true);
AttachGraphics(pGraphics);
// NOW I NEED TO "create it"
mPianoRoll(this, 8, 8, kParamIDPianoRoll);
}
but of course I can't do mPianoRoll(this, 8, 8, kParamIDPianoRoll);
.
I'd like to avoid pointer for this, since on "deleting" I got weird errors, and because (since I don't need pointers), I guess it's better to not use them and dynamic memory allocation.
I can't use Member Init List, since it will be called/used before the Task within the MIDITest
CTOR.
What's the way?
Upvotes: 0
Views: 59
Reputation: 409404
If the construction of mPianoRoll
depends on some of the "tasks" that you perform in the constructor, then you could simply use the assignment operator and do e.g.
mPianoRoll = IPianoRoll(this, 8, 8, kParamIDPianoRoll);
Another possible solution is to make the "tasks" separate classes and have instances of them in the MIDITest
class or have MIDITest
inherit from them (as mixins), and use the construction initializer list for all initialization and tasks, ending with the mPianoRoll
construction in the list.
Upvotes: 1
Reputation: 206707
One option is to initialize mPianoRoll
with as much information as you have in initializer list and then update its state in the body of the constructor.
MIDITest::MIDITest(IPlugInstanceInfo instanceInfo) : IPLUG_CTOR(kNumParams, kNumPresets, instanceInfo),
mPianoRoll(this, 8, 8, <not sure what this would be> {
// SOME TASKS
IGraphics *pGraphics = MakeGraphics(this, GUI_WIDTH, GUI_HEIGHT, PLUG_FPS);
pGraphics->HandleMouseOver(true);
AttachGraphics(pGraphics);
// update the state of mPianoRoll.
mPianoRoll->someSetFunction(kParamIDPianoRoll);
}
Upvotes: 0
Reputation: 4668
You could move the other tasks into a base class. Base classes constructors will get processed first.
You could as well move them into a function, make it return a value, and use it to explicitly initialize another member in the initialization list.
Otherwise, it seems the other tasks are something the member variable is dependant upon, hence it may make sense to move it inside that member variable, or inside a wrapper object (that would be then normally initialized).
Upvotes: 1