473,799 Members | 2,988 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Upwards-casting question

Hello,

Ok, I'm having a problem when trying to make the classes I make a bit
more generic. Consider this:

I have one class, ClassA. This defines some interface methods, and
ClassB inherits from it, and adds some of its own variables. Now, I'm
making a library, which accepts ClassA.

Now the problem is, this library also creates a lot of threads. I want
each of these threads to contain their own instance of ClassA ( so
indirectly, ClassB ). But since there's no way for each of these threads
to know the size of ClassB, it seems impossible to me to let each of
those classes each have a unique instance of ClassB. Ofcourse, I could
use pointers to solve this problem, but this will only work if I create
a new pointer of ClassB for each of those threads /before/ calling the
library that creates those threads. But this isn't really a solution,
since the number of threads should not be known by the calling class.

Any idea at all how I can solve this problem using a somewhat elegant
solution ?

Thanks in advance for any responses!

Regards,

Leon Mergen
http://www.solatis.com/

Sep 7 '05 #1
2 1506
Leon Mergen wrote:
Ok, I'm having a problem when trying to make the classes I make a bit
more generic. Consider this:

I have one class, ClassA. This defines some interface methods, and
ClassB inherits from it, and adds some of its own variables. Now, I'm
making a library, which accepts ClassA.
What does it means "accepts ClassA"? Functions that have 'ClassA' as
arguments? Passed by _value_? If so, your effort to create 'ClassB' is
wasted. If you pass by value, the portion that is 'ClassB' is removed,
and only 'ClassA' remains.
Now the problem is, this library also creates a lot of threads. I want
each of these threads to contain their own instance of ClassA ( so
indirectly, ClassB ).
No, if it "contains" an _instance_ of 'ClassA', it will certainly *not*
contain 'ClassB' directly or indirectly. Read about *slicing*.
But since there's no way for each of these threads
to know the size of ClassB,
Why do you say that it's impossible? You can tell them, can't you?
it seems impossible to me to let each of
those classes each have a unique instance of ClassB.
That's ridiculous. You just said that _you_ are making that library. You
are free to do as you please, so you can tell that library to use 'ClassB'
instead of 'ClassA', can't you?
Ofcourse, I could
use pointers to solve this problem, but this will only work if I create
a new pointer of ClassB
No, not "create a new pointer", create an object, an instance of 'ClassB'.
for each of those threads /before/ calling the
library that creates those threads.
No, you don't have to do it _before_. You can supply a function that each
thread would call to create a new instance of 'ClassB'.
But this isn't really a solution,
since the number of threads should not be known by the calling class.

Any idea at all how I can solve this problem using a somewhat elegant
solution ?


Use a factory. Let your library (threads) know what function to call to
get another object of type [what they know as] 'ClassA'. Inside that
factory function create an object of type 'ClassB' and return a pointer to
'ClassA'.

V
Sep 7 '05 #2
On Wed, 2005-09-07 at 13:29 -0400, Victor Bazarov wrote:
Ok, I'm having a problem when trying to make the classes I make a bit
more generic. Consider this:

I have one class, ClassA. This defines some interface methods, and
ClassB inherits from it, and adds some of its own variables. Now, I'm
making a library, which accepts ClassA.


What does it means "accepts ClassA"? Functions that have 'ClassA' as
arguments? Passed by _value_? If so, your effort to create 'ClassB' is
wasted. If you pass by value, the portion that is 'ClassB' is removed,
and only 'ClassA' remains.


Yes, I understood that - and as I also understand it, the only way for a
ClassA to 'save' the additional contents it had from ClassB, is to pass
it as a pointer or reference. And yes, I mean functions that accept
ClassA as arguments.
Now the problem is, this library also creates a lot of threads. I want
each of these threads to contain their own instance of ClassA ( so
indirectly, ClassB ).


No, if it "contains" an _instance_ of 'ClassA', it will certainly *not*
contain 'ClassB' directly or indirectly. Read about *slicing*.


I know about slicing, that's why I'm asking this question.

But since there's no way for each of these threads
to know the size of ClassB,


Why do you say that it's impossible? You can tell them, can't you?


D'oh. That's true.
it seems impossible to me to let each of
those classes each have a unique instance of ClassB.


That's ridiculous. You just said that _you_ are making that library. You
are free to do as you please, so you can tell that library to use 'ClassB'
instead of 'ClassA', can't you?


Yes, but this is a shared library shared between client code and server
code - and heck, I even might want to re-use this library in the future.
That's why I'm doing the generic approach, as I said in the begin of my
message.
Ofcourse, I could
use pointers to solve this problem, but this will only work if I create
a new pointer of ClassB


No, not "create a new pointer", create an object, an instance of 'ClassB'.


Ok, rephrase to "Create a new instance of ClassB, and let the pointer to
that instance of ClassB be casted to a pointer of ClassA".
for each of those threads /before/ calling the
library that creates those threads.


No, you don't have to do it _before_. You can supply a function that each
thread would call to create a new instance of 'ClassB'.


Hmmmm, ok, sounds interesting....
But this isn't really a solution,
since the number of threads should not be known by the calling class.

Any idea at all how I can solve this problem using a somewhat elegant
solution ?


Use a factory. Let your library (threads) know what function to call to
get another object of type [what they know as] 'ClassA'. Inside that
factory function create an object of type 'ClassB' and return a pointer to
'ClassA'.


Ahhh, yes, that sounds like a good solution! Thanks, I'll look into the
factories...

Regards,

Leon Mergen
Sep 7 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1483
by: 4Space | last post by:
As a company, we have a frightening amount of legacy code. A couple of years ago, we started to move towards .NET, so ended up wrapping a lot of native C++ libraries with managed code. Now, one of .NET's cool things is the GAC (Global Assembly Cache). One of the not so cool things about the GAC, is that things that go into it have to have their dependencies in the GAC (hacks aside). Recently, I've been weaving the dependency inversion...
16
4108
by: Lindon | last post by:
I've been bouncing back between my studies in C/C++ and have found that I like parts of both languages... I find that pointer arithmetics and arrays in C come extremely naturally to me, yet I can't get passed the fact that C structures don't even compare to C++ classes (no constructors, no member functions, of course no inheritance), there's no function overloading, and the good GUI tools (and other library) seem to be all for C++. But I...
10
2582
by: Simon Elliott | last post by:
If I have two classes: class bar { public: void RunTheBar(void); }; class foo {
10
1722
by: rupert.breheny | last post by:
I'm trying to put together a little navigation bar and have a MAIN and then a SUB navigation which has visibility triggered with Javscript. The problem I have is that In firefox I can colour both levels of navigation a differently, but in Explorer the RED of the A element in the "navSub" DIV jumps up a level and cascades to the preceeding "nav" DIV I though cascading was supposed to happen downwards not upwards. Can I have two different...
26
808
by: Kai Jaensch | last post by:
Hello, i am an newbie and i have to to solve this problem as fast as i can. But at this time i don´t have a lot of success. Can anybody help me (and understand my english :-))? I have a .txt-file in which the data is structured in that way: Project-Nr. ID name lastname 33 9 Lars Lundel 33 12 Emil Korla
0
2870
by: PeteCresswell | last post by:
I'm doing the bookmark thing to force the current record in a subform and it works....almost. Problem is that the subform then scrolls so that the newly-selected field is at the very top of the form. We don't want that. Instead, we want the from to be scrolled to the very top (there are a limited number of rows...so there's no problem with the newly-selected row being somewhere off the bottom of the form). Anybody done this?
5
1886
by: Nitin Bhardwaj | last post by:
Sorry I made a typo in the code The correct one is like this : #include <stdio.h> int main(void) { int *buf1 = malloc(10*sizeof(int)); int *buf2 = malloc(10*sizeof(int)); printf(buf1 > buf2 ? "Downwards" : "Upwards");
1
1655
by: sujithegr8 | last post by:
HIii... Its me sujith i've done something with AJAX. but for the rest i need someo ones help.. i've done half the work. there are two tables. "ajax1" and "ajax2" (check db.sql)
5
2224
by: jbrumbau | last post by:
Hello, I'm trying to generate a report with a subform, with the Can Grow property set to Yes. In the attached picture, the list of bidders is the subform, to the right of where you see the row with the dates with "P" next to them. My problem is that the bidders cascade down instead of cascading up, resulting in a gap between the "P" row and the "F" row. The reason is because I'd rather have the gap be between individual packages (they're...
0
1571
by: dentaku | last post by:
After I upgraded from FOP 0.20.5 to FOP 0.95, suddenly all texts in the tables moved towards the top. With FOP 0.20.5, the text was centered in a table cell that the CAPITAL letters were vertically centered. With FOP 0.95 the text is centered in a table cell that the LOWER CASE letters are vertically centered, i.e. the whole text moved towards the top of the cell. I found a workaround by enclosing the text in <fo:inline...
0
9686
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10026
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.