passing derived type to client who only knows base type

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
 
V

Victor Bazarov

Client side; knows nothing of Derived:

class Base
{
...
virtual void do_something()
;
} ;

#include "Base.h"
void main() {

int 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*?

It cannot, most likely.
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.

You need indirection. Don't ask 'Base' for the 'instance'. Use some
third class, residing in the library, which you would ask for the
'instance' of "Base". The real one would give you the instance of
'Derived', pretending it's a "Base". It's called a "factory" or some
such. It doesn't matter if it returns the same pointer every time.
The principle is that your "factory" is independent from 'Base' (i.e.
Base doesn't know about it), and is supplied along with 'Derived' by
your library.

V
 
R

reckless2k

You need indirection. Don't ask 'Base' for the 'instance'. Use some
third class, residing in the library, which you would ask for the
'instance' of "Base". The real one would give you the instance of
'Derived', pretending it's a "Base". It's called a "factory" or some
such. It doesn't matter if it returns the same pointer every time.
The principle is that your "factory" is independent from 'Base' (i.e.
Base doesn't know about it), and is supplied along with 'Derived' by
your library.


Yes! That's it :) I have used factories before, but for some reason I
didn't see that here.

Thank you!

Jeremy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top