I'm making some Manager class like this.
#pragma once
#include <iostream>
#include <unordered_map>
using namespace std;
class TestMgr
{
public:
TestMgr() = default;
~TestMgr() {
for (auto iter = m_test.begin(); iter != m_test.end(); )
{
delete iter->second; // void* doesn't call destory function
iter = m_test.erase(iter);
}
}
template<typename T>
T* DoSomething()
{
CObject<T>* someClass = (CObject<T>*)m_test[typeid(T).hash_code()];
if (someClass == nullptr)
{
m_test[typeid(T).hash_code()] = new CObject<T>();
}
return someClass->DoSomething();
}
unordered_map<size_t, void*> m_test;
};
I want to save 'Template' classes in containers like 'unsorted_map' (doesn't matter with container) And want to call a destroy function. But I have been saving the pointers by void* So if I delete void* , destroy function don't work! I'm will to save types, so I can convert back to its own type. like this
delete (Ctest*)iter->second;
how can I convert back to my Template type. I need some help Thank you!
The easiest solution would be a virtual CObjectBase::~CObjectBase
. Now you don't need a void*
anymore.