473,387 Members | 1,536 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,387 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 1694
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.