vendredi 23 février 2018

Statically allocated objects which don't die at out of scope?

What is a good way to allocate an object statically which does not die at end of scope?

For some class C with a constructor C() I have mostly used

C* lPtr = new C();

or

C& lPtr = *new C();

depending on what to return. But this is dynamically allocated which is not what I want.

I can write:

C lObj();

Which would allocate statically the memory of lObj, but then lObj will be destructed when leaving scope.. which I don't want either.

Do there exist any design patterns to build objects statically which survive falling out of scope?

Aucun commentaire:

Enregistrer un commentaire