I’m hoping to count how many times a pointer is being used. I have a map:
static std::map<unsigned int, unsigned int> counters;
When I want to insert a new value to it i’m using it like this:
template<class T>
MyClass::addPointer(T * tPtr){
counters[((unsigned int) tPtr)]++;
}
Is it OK and safe to do a cast like this? It’s not an expensive operation etc.?
Also, is this a suitable way to ensure each pointer only gets one count?
Thanks
IMO, you really don’t need to cast it to
unsigned int. You can have themapwithvoid*:Also a null check is important here:
Rest is fine.