Re: Useless use of smart pointers
class ObjStore1 {
// ...
typedef std::map< std::string, const Obj* > store_type;
store_type store;
// ...
};
In this implementation, ~ObjStore1 needs to delete the Objs:
ObjStore1::~ObjStore1() {
store_type::iterator it;
for ( it = store.begin(); it != store.end(); ++it ) {
delete it->second;
}
};
A "simple" modification could be using std::tr1::shared_ptrs in the
map:
class ObjStore2 {
// ...
typedef std::map< std::string,
std::tr1::shared_ptr< const Obj > > store_type;
store_type store;
// ...
};
What have we won? There's no destructor needed anymore, and the get
method gets rid of std::auto_ptr:
However, the runtime memory consumption of your new code has
increased. This is because shared_ptr has two pointer members (at
least boost one does).
Since objects of type Obj are by their definition 'large', the
additional pointer and the additional counter will have little
relative impact.
shared_ptr also allocates an object with a
counter behind the scenes, so for every object managed by shared_ptr
you incur +1 memory allocation.
Since the allocation of T and the allocation of the counter are
one-one, a single memory allocation will suffice in a good
implementation.
IMO, the original code is good enough for its purpose and introducing
smart pointers does not make it any better. A good design is not when
there is nothing left to add, rather when there is nothing left to
remove.
IMO, removal of the destructor (and hence also special treatment of
copy and assignment) is most worthy. If the types of the members are
specific enough then the C++ type system and the default members will
take care of everything.
Any member with a plain T* in its declaration is not really specific
enough. We don't know what we can and/or should do to the T* without
examining all contexts. The contract is implicit. So that is also
worth removing.
By being specific about the contract, using the right kind of smart
pointer, in the declaration of the relationship between ObjStoreN and
Obj lifetimes, we remove two evils.
Nick
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]