   [c++]collection and template
  Hy.I must implement a collection class(CCollection) that store some mesh.These mesh are derived from CMeshBase and are of type:CMesh,CMeshTexture,CSkinMesh and so one.I read in the c++ programming language of stroustrup that everytime you implement a class collection, template are the best solution , is true or i 'm confusing?This collection class must have a shader object that change according to the type of the mesh stored at the instancing of CCollection  that resolve the tipe of the mesh class stored in the collection and the type of the shader also in the collection as a pointer(Shader* pShader).Can you advice me a good solution ?is the case of using templates?   
  I'm not sure I'm clear on your requirements, but you should be able to do something simple like create a std::vector<boost::shared_ptr<CMeshBase> > to hold your pointers. Then you can set up virtual functions in your mesh class to handle the functionality that needs to change based on mesh type.   ;  Templates are compile-time polymorphism. Public inhertiance is run-time polymorphism. You require the second - but using the first as well you can create more generic code.If you have boost you can use a boost::ptr_vector<CMeshBase>; if not, but you have a compiler that supports tr1 you can use a std::vector<std::tr1::shared_ptr<CMeshBase> >. If you have neither, consider wrapping a std::vector<CMeshBase*>.   ;Quote:Original post by ApochPiQThen you can set up virtual functions in your mesh class to handle the functionality that needs to change based on mesh type.I'm not understand at all, can you post me one example of these virtual function?;This should get you started.   ;  thanks, i re-read the first question and i'm find the answer.but why shared pointers?by.   ;  Consider the following code:void function(){    CMesh *pMesh = new CMesh();}You obviously leak memory here because you don't call delete on pMesh. But if you use a boost::shared_ptr, you don't leak memory:void function{    boost::shared_ptr<CMesh> pMesh( new CMesh() );}boost::shared_ptr implicitly calls delete on the pointer you store when it is destroyed. You won't need to worry about manually deleting the memory anymore.*Edit* You could also use std::smart_ptr, however a smart pointer has exclusive ownership, which means that if you assign one smart pointer to another, the assigned one will be empty. Due to this design, a smart_ptr can not be used with a container:std::vector< std::smart_ptr<bool> > vec;vec.push_back( std::smart_ptr<bool>( new bool( false ) ) ); // This won't compileThat's why Nitage and ApochPiQ proposed a shared_ptr: You can use them with any container.[Edited by - SiS-Shadowman on May 15, 2009 2:24:14 AM]