quetzalfir
quetzalfir

Reputation: 568

C++, pattern to initialize classes

In my code I'm implementing so many different classes that need to be initialized once the program's process is activated.

I am currently initiating manually each class, but they are so many, I thought that I could inherit from some class and each object created would be saved in a vector and could initalize the class from the object (base class) created, but I only need to initalize the class once, and even though I can set a flag for that, I dont think its a good solution.

Other solution that I thought is implement a kind of initializer on each class (static variable) that I have to initalize, same implementation, save the init object per class in a vector, and then init all the classes from the initializer class, but I have to pass to that object a lot of parametres and even functions. so is a good solution but a bad implementing, think so.

so Do you know for some good pattern to initialize classes?

EDIT: EXAMPLE:

With Android and OpenGL, each time the app is not OnResume(), the Opengl context is destroyed, and OnResume() the context is recreated, and for any object that I need to render, I have a class for example 'Square class' that draws squares, and for each square object I have static variables in the 'Square class', that each square has access to be rendered properly with opengl, like buffers or shaders, etc, so any time the context is recreated, I need to reinitialize the class static variables in order that the object be rendered.

Upvotes: 0

Views: 909

Answers (2)

GhostCat
GhostCat

Reputation: 140543

Maybe I get this totally wrong, but at least in the Java world, one way of solving this topic is by using dependency injection via frameworks that take care of init'ting your objects, based on configuration information that you have to provide.

Meaning: you completely separate your actual "business logic" and the task of "establishing all the required objects". You don't do "new" any more in your code; all the important objects are created for you "by magic".

One example of such a framework for C++ would be fruit, which is actually inspired by Google's guice thing for java.

Upvotes: 1

mcdowella
mcdowella

Reputation: 19611

One standard pattern for initialisation in C++ is the nifty-counter pattern. See http://www.petebecker.com/js/js199905.html. To summarise, in your header file you define an object with a constructor, so this constructor will be executed once for each .cpp that includes the header file. This constructor increments a global shared counter. If the counter goes from 0 to 1 you run initialisation code. Similarly, the destructor decrements the counter, and de-initialises or destroys stuff when it goes from 1 to 0.

This works well with the only C++ guarantee about order of initialisation, which is that, within the same file, objects are constructed and initialised from top to bottom. If people have multiple .cpps which reference each other via header files, the first thing that gets initialised will be defined in a header that does not include anything else that needs initialisation first, which should be the something that does not depend on anything else.

Upvotes: 1

Related Questions