Hi
My app contains several modules (big classes) like network io, data storage, controls, etc. Some of them can cooperate. What would be a good way of declaring and binding the modules? I see several possibilities:
1) All modules declared as global, so we have in main.cpp
#include ...
ModuleA a;
ModuleB b;
ModuleC c;
and if a wants to talk to module c for example, we will have in a.cpp the following:
#include "c.hpp"
extern ModuleC c;
and so on;
2) All modules declared in main(), so they are local. The bindings are made in constructors:
int main() {
ModuleC c;
ModuleA a(c);
ModuleB b;
}
but in this way would be hard to bind objects which want each other ( a(c), c(a) )
3) First phase: declare locally, second phase: bind with pointers:
int main() {
ModuleA a;
ModuleB b;
ModuleC c;
a.Connect(&b);
b.Connect(&a);
c.Connect(&a, &b);
}
Is there a better way? I'd like it to be in cpp style. Third way keeps pointers which is a bit confusing (though there won't be problems with validness of pointers though modules lives all the time, but still) and has two-phase initialization, which doesn't guarantee that we won't forget to init some module, and oops -- an invalid pointer. The second way (as i think) may crash all the idea if some objects would need cross-binding. The first way seems to be natural (since modules represent the app itself), but isn't it a bad style? I saw some projects where the modules were declared in some universe class and they cooperate via this universe just like it woule be if all of them are global. What do you think?
Thanks.