473,813 Members | 4,172 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1715
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********@gmai l.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_ca st 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********@gmai l.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********@gmai l.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
7055
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 os.kill(self.popen.pid, signal.SIGTERM) Everything works fine on MacOSX. However, I'm doing a port to Solaris (so I can run it on my web site) and find that the child processes are not stopping! Solaris is creating TWO new processes: one for the SHELL...
5
15070
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
9662
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 assigned to a variable and output using document.write. (Note, there is no submit button or other form elements. Basically
18
4897
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) p.stdin.write("hostname\n") however, it doesn't seem to work. I think the cmd.exe is catching it.
7
2616
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 children - when a child is fork()ed its pid is added to the linked list. I then have a SIGCHLD handler which is supposed to remove the pid from the list when a child exits. The problem I'm having is that very very occasionally and seemingly...
18
3718
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 be written to, since its contents are undefined. The program is allocating a new piece of data, and the previous contents aren't relevant. This memory
15
4901
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
4051
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? I've been leaning towards threads, I'm going to say why. Processes seem fairly expensive from my research so far. Each fork copies the entire contents of memory into the new process. There's also a more expensive context switch between...
3
3521
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
9607
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10665
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10406
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10420
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9221
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
7681
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
6897
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();...
2
3881
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3029
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.