Go Back   Talk Root - PC Hardware, Software and Web Development forums > Internet & Website Management > Programming Discussion > C Languages > C++



Reply
 
LinkBack Thread Tools Search this Thread Rate Thread
  #1  
Old 04-20-2004, 08:42 AM
cass
Guest
 
Posts: n/a
Default Is a list template class needed?

Both class A and B inherites from class C. A std::list is used to contain a
list of objects of class A or a list of objects of class B, but not both
types of classes at the same time.

How to use template to realize this? Thanks!


Reply With Quote
  #2  
Old 04-20-2004, 08:42 AM
Siemel Naran
Guest
 
Posts: n/a
Default Re: Is a list template class needed?

"cass" <casscure@knowhouse.com> wrote in message news:mIHgc.51170

> Both class A and B inherites from class C. A std::list is used to contain

a
> list of objects of class A or a list of objects of class B, but not both
> types of classes at the same time.
>
> How to use template to realize this? Thanks!


If you use a list<C> you can hold both A and B objects in the same list.
But you don't have to take advantage of this -- ie. you can set it up so
that all the objects will be A or all will be B.

Actually, in C++ you have to use something like
std::list<boost::shared_ptr<C> > and ensure that C::~C() is virtual.

In your list insert function you can enforce all items have the same type.

class mylist : private std::list<boost::shared_ptr<C> > {
typedef boost::shared_ptr<C> smart_pointer_type;
typedef std::list<smart_pointer_type> list_base;
public:
class inconsistent_type { };
typedef list_base::size_type size_type;
size_type size() const { return list_base::size(); }
C& back() { return *list_base::back(); }
const C& back() const { return *list_base::back(); }
void push_back(std::auto_ptr<C> newobj) {
if (size()) {
if (typeid(back() != typeid(*newobj)) throw inconsistent_type();
}
list_base::push_back(smart_pointer_type(newobj.rel ease()));
}
};

Surely, there might be other ways to do it.


Reply With Quote
  #3  
Old 04-20-2004, 08:42 AM
Kevin Goodsell
Guest
 
Posts: n/a
Default Re: Is a list template class needed?

Siemel Naran wrote:
> "cass" <casscure@knowhouse.com> wrote in message news:mIHgc.51170
>
>
>>Both class A and B inherites from class C. A std::list is used to contain

>
> a
>
>>list of objects of class A or a list of objects of class B, but not both
>>types of classes at the same time.
>>
>>How to use template to realize this? Thanks!

>
>
> If you use a list<C> you can hold both A and B objects in the same list.


I suppose you meant list<C*> here.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Reply With Quote
  #4  
Old 04-20-2004, 08:42 AM
Siemel Naran
Guest
 
Posts: n/a
Default Re: Is a list template class needed?

"Kevin Goodsell" <usenet2.spamfree.fusion@neverbox.com> wrote in message
news:HwKgc.2081
> Siemel Naran wrote:


> > If you use a list<C> you can hold both A and B objects in the same list.

>
> I suppose you meant list<C*> here.


Yes. Maybe my wording was excellent, but in the next paragraph I wrote the
following:

>>Actually, in C++ you have to use something like
>>std::list<boost::shared_ptr<C> > and ensure that C::~C() is virtual.


Raw pointers would have don't have auto destruct and deep copy, though we
could build that into our container, but what a hassle.



Reply With Quote
Reply

Bookmarks
Sponsored Links
FatCow $88 Plan for $66 only Host Unlimited Domains on 1 Account Professional Hosting from Just Host


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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 On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On



All times are GMT -7. The time now is 10:40 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
© 2008 TalkRoot.com