I have a micro controller c++ project where I declare as much as possible globally instead of newing objects. In fact, I am not using the new keyword anywhere.
I now ran into the situation where the order of the __libc_init_array is causing issues for me. I hope I can explain my situation well enough for somebody to point out if I am doing something horribly wrong, or if I can control the problem at hand.
I have a lot of classes, where dependencies are injected through the constructor. The objects are constructed when startup code calls into __libc_init_array. I now have one situation where the the constructor of class B is called before the constructor of class A is called, while B depends on A. So when the constructor of B uses any member of 'uninitialized' A, the app hard faults.
Is there a way to enforce the order of constructors called by __libc_init_array? Or did I make a horrible mistake relying on this behavior? Or... is it a bad idea to use members of dependencies in the constructor all together in c++?
I realize when I would new all objects I would have 100% control over this behavior construction order, but it's quite a bit of work to refactor that.
For now I added an initialize function for the single case where the dependency order is a problem, which solves the problem but doesn't feel like an appropriate solution either.
I tried to google for better solutions but keep coming up with non-related issues.
I meant something like this: