C++: access to container of shared_ptr should return raw or shared ptr? -


if use container of shared_ptrs , explicitely allow access elements, should return shared_ptrs or raw pointers if intend container 1 responsible "cleaning up"?

class container { private:     std:vector<shared_ptr<foo> > foo_ptrs;  public:     shared_ptr<foo> operator[](std::size_t index) const {}; // or     foo* operator[](std::size_t index) const {}; }; 

is there reason return shared_ptrs in such situation, or raw pointers ok?

greets!

return reference

only return shared_ptr if intend accessors share in lifetime management. valid design said container solely responsible cleanup. shared_ptr implementation detail of containter, fact there vector of shared_ptrs used implement container shouldn't exposed through interface.

don't return pointer unless make sense null container. not. user wants access i-th element of container , reference perfectly.

what want std::vector<std::unique_ptr<foo>>. container 1 managing memory , says in member declaration. else has no business knowing implementation.

take @ boost.pointainers if you're unwilling or unable use c++0x.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -