R
reckless2k
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 pointer returned by Base::instance() to be
of type Derived*? I can't simply define the Base::instance() method on
the Lib side, because I'll get a link error; I can't set the static
_instance variable explicitely on the Lib side, because C++ can't
guarantee initialization order of static variables. One solution would
be to instantiate a global Derived on the Lib side calling the ctors of
itself and it's Base, which set the _instance value.. but this idea
sucks; I want a real singleton, if possible. The problem is that I
need _instance to be instanciated with a Derived* AFTER the main entry
and BEFORE the virtual do_something() resolves.
Any help would be appreciated! Thanks
Jeremy
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 pointer returned by Base::instance() to be
of type Derived*? I can't simply define the Base::instance() method on
the Lib side, because I'll get a link error; I can't set the static
_instance variable explicitely on the Lib side, because C++ can't
guarantee initialization order of static variables. One solution would
be to instantiate a global Derived on the Lib side calling the ctors of
itself and it's Base, which set the _instance value.. but this idea
sucks; I want a real singleton, if possible. The problem is that I
need _instance to be instanciated with a Derived* AFTER the main entry
and BEFORE the virtual do_something() resolves.
Any help would be appreciated! Thanks
Jeremy