   Using callback to communicate between modules
  I'm having a problem getting the various parts of my program to talk to each other. The way I have it set up is that there is a central structure called the core, which manages both the data and the modules, and the modules them selves, which contain the actual functionality. (Graphics, sound, input, AI, etc.)The core creates the module objects and can run them by calling their public classes, but the only way that the modules can talk back is through a callback via function pointer that the core passes them at startup.My problem seems to be that in order for the callback to work, it must be a static function. As a result, any data that it calls must be static as well (as I understand it) But if I just make everything static, the program will compile, but not link.I could always make the core into a plain set of fuctions, not as a class, but that seems to me to be a very messy solution.#include "iDataManager.h"  //interface for the datatype class#include "iModule.h"  //interface for the module class#include "message.h"  //message datatype, built on the iDataManager interfaceclass cCore{public:    int init();    void run();    void shutdown();    void checkMsg();   //cycle through all the messages    int registerModule(iModule* IN);    static iDataManager* registerData(iDataManager* IN);  //this is the callback in questionprivate:    vector<iModule*> modules;//an array that points to all the registered modules    vector<iDataManager*> data;   //an array of all the registered data classes    message* msg;  //pointer to the message system    bool bQuit;    //flag to exit the program};
  I think it's possible to use pointers to methods in C++, but that's not a very elegant solution IMO.As I see it, you have two options:1) Add a virtual method to the module class, and have a class in the core that inherits from it and implements the virtual method. The virtual method now plays the role of the callback.class someClassInLogic{public:...  virtual iDataManager* callback(iDataManager*);};class someClassInLogicRegistersWithCore{public:  someClassInLogicRegistersWithCore(cCore* core): core(core) {...}...  iDataManager* callback(iDataManager* IN) { return cCore->registerData(IN); }private:  cCore* core;};2) Use function objects.class Callback{public:  virtual iDataManager* f(iDataManager* IN);};class someClassInLogic{public:...  void addCallback(Callback* c) { callbacks.push_back(c); }private:  std::vector<Callback*> callbacks;};class CallbackCore{public:  CallbackCore(cCore* core): core(core) {}  iDataManager* f(iDataManager* IN) { return core->registerData(IN); }private:  cCore* core;};