defering the implementation of Base::instance() to Derived w/o link errors
My simple client knows only about a Base Singleton and its interface;
but I need to be able to set the _instance static variable to a Derived
type to change the behavior of the client WITHOUT the client knowing
anything about Derived.
I wish I could defer the implementation of Base::instance() to
somewhere in Derived, but I need the definition of instance() to link
properly...
===================
Client side; knows nothing of Derived:
-------------------------------------
class Base { /* ... */ virtual void do_something(); };
#include "Base.h"
void main() { Base::instance()->do_something(); }
===================
Lib side; knows nothing of main():
-------------------------------------
#include "Base.h"
class Derived : public Base { /* ... */ void do_something(); };
===================
How can I set the _instance variable to be of type Derived* without
informing the client about Derived? I have tried setting _instance in
the ctor and createing a global Derived in Derived.cc, which forces the
type, but it is not dynamically allocated and it is not really a
singleton (I could create as many as I wanted and I would lose the
previous _instance values) I thought about setting the Base::_instance
value on the Lib side, but there is no guarantee for instantiation
order of statics in c++, so I didn't do it... there must be an easy way
to do this! I just can't see it. Any help would be greatly
appreciated.
Thanks
Jeremy
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]