471,585 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,585 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 1625
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Bob Swerdlow | last post: by
13 posts views Thread by Stumped and Confused | last post: by
18 posts views Thread by jas | last post: by
18 posts views Thread by jacob navia | last post: by
15 posts views Thread by Dirk Reske | last post: by
35 posts views Thread by Carl J. Van Arsdall | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by Anwar ali | last post: by

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.