472,995 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Write a function processes a list

I have 2 classes, A, B and B is a child of A.

and I have a function which processes a list of A.

void func(list<A> alist) {
// processing list of A
}

The problem is func() won't able to handle a list of B even B is a
child of A.

What can I do? The only way I can think of is making func a function
template, so I can plugin both A, and B. But I have a lot of
funcitons like this.

And I java I don't have this problem.
Since both A and B are child of Object and I just pass in a list of
Object
and instead func() I just need to subcast that to A (which will work
for both class A and B (a child of A).

Thanks for any help.

Jan 11 '06 #1
9 1683
dc
Ur problem is object slicing.
Use pass by reference or Ptr in func()
arguments instead of pass by value.

Jan 11 '06 #2
sorry, there is a typo in my original mail.

The function argument is a reference to the list<A>, but I still can't
pass list<B> to that function.

void func(list<A>& alist) {
// processing list of A

}

Jan 11 '06 #3
dc wrote:
Ur problem is object slicing.
Use pass by reference or Ptr in func()
arguments instead of pass by value.


The problem is not object slicing. The problem is that - when B
inherits from A -
"a B is a A",
but "a list<B> is not a list<A>"

Hence, if a function is declared to take A (or A* or A&), an object of
type B (or a pointer or reference thereof) can be passed to the
function. But when a function is declared to take
list<A> or list<A*> etc, a list<B> or list<B*> cannot be passed to that
function, simply because list<B> is not a list<A>.

To the OP -This is correct as far as the semantics of list is
concerned. (Since otherwise you would be able to push non-B objects
inside the list through such a function.) The easiest way to deal with
the situation is to overload the function for list<B>.

There might be better solutions though.

Jan 11 '06 #4
og********@gmail.com wrote:
I have 2 classes, A, B and B is a child of A.

and I have a function which processes a list of A.

void func(list<A> alist) {
Bad ideat to pass the list by value. If the list changes, you
need pass it by reference, if it doesn't, pass it by reference
to const.
// processing list of A
}

The problem is func() won't able to handle a list of B even B is a
child of A.
That's correct. list<B> and list<A> are only the same type if A and
B are the same type.
What can I do? The only way I can think of is making func a function
template, so I can plugin both A, and B. But I have a lot of
funcitons like this.
That's one of the main reasons to make your functions templates.
And I java I don't have this problem.
So? In Java you have plenty of other problems.
Since both A and B are child of Object and I just pass in a list of
Object
and instead func() I just need to subcast that to A (which will work
for both class A and B (a child of A).


You can have a list<A*> and store pointers to B in it, but it's
a bit more work. Trust me, it's much more elegant with templates.

V
Jan 11 '06 #5
dc
Oops sorry, not at all object slicing.
With use of Ptrs,I meant something like this:

class A{
virtual void process(){
..............
}
}

class B : public A{
void process(){
}
}

void func(list<A>* alist) {
// processing list of A
A *x=&(alist->front());
x->process();
}

calling func:
list<A> x;
list<B> y;
func(&x);
func((list<A>*)&y);

I hope this helps......

Jan 11 '06 #6
dc wrote:
Oops sorry, not at all object slicing.
With use of Ptrs,I meant something like this: void func(list<A>* alist) {
// processing list of A
A *x=&(alist->front());
x->process();
}

calling func:
list<A> x;
list<B> y;
func(&x);
func((list<A>*)&y);

I hope this helps......


I am afraid that this will invite other problems.

The C-style cast that you used is actually a reiniterpret_cast whose
meaning is completely implementation defined. As such, you cannot cast
list<B>* to list<A>* (unless you decide to reinterprete the bit
pattern) since list<B> is _by no means_ a list<A>.

One serious problems that can arise due to this is that whenever
'alist' is dereferenced, it will be sliced off to list<A>. Also the
function func will allow operations like pushing an object derived from
A (but not of type B) inside the list which originally had only the
elements of type B.

Jan 11 '06 #7
Is a list <A*> better than a list of <A&> (a list of A reference)?

Jan 11 '06 #8
og********@gmail.com wrote:
Is a list <A*> better than a list of <A&> (a list of A reference)?


References are not objects and cannot be stored in containers.

V
Jan 11 '06 #9
On 2006-01-11 15:52:59 -0500, og********@gmail.com said:
Is a list <A*> better than a list of <A&> (a list of A reference)?


STL containers of references are not possible.

--
Clark S. Cox, III
cl*******@gmail.com

Jan 11 '06 #10

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

Similar topics

6
by: Bob Swerdlow | last post by:
My application starts up a number of processes for various purposes using: self.popen = popen2.Popen3("/usr/local/bin/python -O "myscript.py") and then shuts them down when appropriate with...
5
by: Roger | last post by:
I would like to get a list of running processes on a remote machine. How is this possible via VB.Net? Is it possible? Can someone point me in the right direction. thanks, rog
13
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
18
by: jacob navia | last post by:
In C, we have read-only memory (const), read/write memory (normal data), and write only memory. Let's look at the third one in more detail. Write only memory is a piece of RAM that can only...
15
by: Dirk Reske | last post by:
Hello, why doesn't this code work correctly? private int GetCpuUsage(Process proc) { DateTime time1,time2; TimeSpan timediff; double cpu1,cpu2,cpudiff;
35
by: Carl J. Van Arsdall | last post by:
Alright, based a on discussion on this mailing list, I've started to wonder, why use threads vs processes. So, If I have a system that has a large area of shared memory, which would be better? ...
3
by: xxs | last post by:
I have writen some codes as follow: #include <windows.h> #include <tlhelp32.h> #include <stdio.h> // Forward declarations: BOOL GetProcessList( ); BOOL ListProcessModules( DWORD dwPID...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.