473,321 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

passing derived type to client who only knows base type

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
2 1426
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
7
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException...
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
3
by: Jasleen | last post by:
I want to access the protected member hresult of the Exception class, so I derived my own class from ApplicationException and SystemException. public class LoggedException : SystemException {...
1
by: Mark McDonald | last post by:
This question kind of follows on from Mike Spass’ posting 10/11/2004; I don’t understand why you can’t declare an implicit operator to convert a base class to a derived class. The text...
6
by: MSDNAndi | last post by:
Hi, I have a baseclass (non-static) with some static and some non-static methods/fields/properties. In the baseclass in one of the static methods I need to do something like " somelogic...
25
by: Jack | last post by:
Hi, Is there a general solution for the following problem: I have an array of instances of class B. Class B is publicly derived from class A. Then I have a class named Buffer that generally...
3
by: Bit Byte | last post by:
Must be the time of the day, but I seem to get my head in a spin over this ... I have a base class B, from which I have a derived class. In class B, i have a (public access) typedef of a...
9
by: fgh.vbn.rty | last post by:
Say I have a base class B and four derived classes d1, d2, d3, d4. I have three functions fx, fy, fz such that: fx should only be called by d1, d2 fy should only be called by d2, d3 fz should...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.