By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,336 Members | 2,307 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,336 IT Pros & Developers. It's quick & easy.

passing derived type to client who only knows base type

P: n/a
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

Sep 16 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
re********@yahoo.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 16 '06 #2

P: n/a
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

Sep 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.