473,396 Members | 1,982 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,396 software developers and data experts.

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 1479
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
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...
16
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...
10
by: Simon Elliott | last post by:
If I have two classes: class bar { public: void RunTheBar(void); }; class foo {
10
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...
26
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...
0
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...
5
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 >...
1
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
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...
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...

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.