ogerchikov@gmail.com wrote:[color=blue]
> 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) {[/color]
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.
[color=blue]
> // processing list of A
> }
>
> The problem is func() won't able to handle a list of B even B is a
> child of A.[/color]
That's correct. list<B> and list<A> are only the same type if A and
B are the same type.
[color=blue]
> 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.[/color]
That's one of the main reasons to make your functions templates.
[color=blue]
> And I java I don't have this problem.[/color]
So? In Java you have plenty of other problems.
[color=blue]
> 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).[/color]
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