Silently binding a variable instance to a class in C++?
- by gct
So I've got a plugin-based system I'm writing. Users can create a child class of a Plugin class and then it will be loaded at runtime and integrated with the rest of the system. When a Plugin is run from the system, it's run in the context of a group of plugins, which I call a Session.
My problem is that inside the user plugins, two streaming classes called pf_ostream and pf_istream can be used to read/write data to the system. I'd like to bind the plugin instance's session variable to pf_ostream and pf_istream somehow so that when the user instantiates those classes, it's already bound to the session for them (basically I don't want them to see the session internals)
I could just do this with a macro, wrapping a call to the constructor like:
#define MAKE_OSTREAM = pf_ostream_int(this->session)
But I thought there might be a better way. I looked at using a nested class inside Plugin wrapping pf_ostream but it appears nested classes don't get access to the enclosing classes variables in a closure sort of way.
Does anyone know of a neat way to do this?