Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 11th, 2006, 05:15 AM
ogerchikov@gmail.com
Guest
 
Posts: n/a
Default 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.

  #2  
Old January 11th, 2006, 05:55 AM
dc
Guest
 
Posts: n/a
Default Re: Write a function processes a list

Ur problem is object slicing.
Use pass by reference or Ptr in func()
arguments instead of pass by value.

  #3  
Old January 11th, 2006, 05:55 AM
ogerchikov@gmail.com
Guest
 
Posts: n/a
Default Re: Write a function processes a list

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

}

  #4  
Old January 11th, 2006, 06:05 AM
Neelesh Bodas
Guest
 
Posts: n/a
Default Re: Write a function processes a list

dc wrote:[color=blue]
> Ur problem is object slicing.
> Use pass by reference or Ptr in func()
> arguments instead of pass by value.[/color]

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.

  #5  
Old January 11th, 2006, 06:05 AM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Write a function processes a list

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


  #6  
Old January 11th, 2006, 06:55 AM
dc
Guest
 
Posts: n/a
Default Re: Write a function processes a list

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......

  #7  
Old January 11th, 2006, 09:05 AM
Neelesh Bodas
Guest
 
Posts: n/a
Default Re: Write a function processes a list

dc wrote:[color=blue]
> Oops sorry, not at all object slicing.
> With use of Ptrs,I meant something like this:[/color]
[color=blue]
> 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......[/color]

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.

  #8  
Old January 11th, 2006, 09:05 PM
ogerchikov@gmail.com
Guest
 
Posts: n/a
Default Re: Write a function processes a list

Is a list <A*> better than a list of <A&> (a list of A reference)?

  #9  
Old January 11th, 2006, 09:25 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Write a function processes a list

ogerchikov@gmail.com wrote:[color=blue]
> Is a list <A*> better than a list of <A&> (a list of A reference)?
>[/color]

References are not objects and cannot be stored in containers.

V
  #10  
Old January 11th, 2006, 09:45 PM
Clark S. Cox III
Guest
 
Posts: n/a
Default Re: Write a function processes a list

On 2006-01-11 15:52:59 -0500, ogerchikov@gmail.com said:
[color=blue]
> Is a list <A*> better than a list of <A&> (a list of A reference)?[/color]

STL containers of references are not possible.

--
Clark S. Cox, III
clarkcox3@gmail.com

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles