What I want to do: run some pre-code when the class instance is used inside the program. This code checks requiremts, etc. And it should be launched only once.
I found that this can be achieved using another object as a static variable inside the constructor. Here is an example of a better image:
class Prerequisites
{
public:
Prerequisites() {
std::cout << "checking requirements of C, ";
std::cout << "registering C in dictionary, etc." << std::endl;
}
};
class C
{
public:
C() {
static Prerequisites prerequisites;
std::cout << "normal initialization of C object" << std::endl;
}
};
What bothers me is that I have not yet seen a similar use of static variables. Are there any flaws or side effects, or am I missing something? Or maybe there is a better solution? Any suggestions are welcome.