Boost logo

Boost Users :

From: Klaus Nowikow (nowikow_at_[hidden])
Date: 2004-08-13 03:03:10


> dp12-ipvi_at_[hidden] wrote:
>
> Use std::list<boost::shared_ptr>

std::list<boost::shared_ptr<tree> >

to be exact ;-)

>
> I hope I spelled all that correctly.
>
> ++PLS

--
Klaus
> 
> -----Original Message-----
> From: goochrules! [mailto:goochrules_at_[hidden]] 
> Sent: Thursday, August 12, 2004 2:02 PM
> To: boost-users_at_[hidden]
> Subject: [Boost-users] polymorphic container/boost::any
> 
> * Replies will be sent through Spamex to boost-users_at_[hidden]
> * For additional info click -> http://www.spamex.com/i/?v=4259897
> I'm quite sure that this problem has been addressed many 
> times before, so I
> suspect it as a better name and I suspect that boost::any could be the
> solution.
> I want to store related types in the same stl container, for 
> instance, this
> is the class heirachy I want:
> class tree { public: void grow() =0; };
> class oak : public tree { public: void grow() { std::cout << 
> "oak grows
> slowly" << std::endl; }; class pine : public tree { public: 
> void grow() {
> std::cout << "pine grows quickly" << std::endl; }; Then, I 
> try to put them
> in a std::list:
> std::list< tree > forest;
> forest.push_back(oak());
> forest.push_back(pine());
> ...and I find out that I can't define tree::grow as abstract. 
>  So I give it
> a dummy implentation -- void grow() { std::cout << "tree grows" <<
> std::endl; --, and try to iterate over the objects:
> for(std::list< tree >::iterator iter = forest.begin(); 
> forest.end() != iter;
> ++iter) {
> iter->grow();
> }
> ...and I don't get what I expect:
> tree grows
> tree grows
> I then think about trying to use a std::list< tree& > and 
> discover that it
> won't compile.  I think briefly consider std::list< tree* > 
> but decide this
> dosen't meet requirements of memory management.
> So I do some searching for 'polymorphic container' and 'polymorphic
> iterator' and come up with boost::any, but I've no luck in 
> making it work.
> Attached is my code.
> In the end, I'm looking for a way to add related types to a 
> stl container
> and not have to do anything complicated to call a method on the type's
> abstract interface and to not have to wory about memory 
> management.  Maybe
> this question is better suited for comp.lang.c++[.moderated]?
> -- Matthew Peltzer
> -- goochrules_at_[hidden]
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
> 

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net