Call destructor via void pointer with templates
<p>I wrote a class to contain my objects in it.
The code is:</p>

<pre><code>class objectPool
{
private:
    struct itemType_{uint count; void* object;};
    std::multimap&lt; std::string,  itemType_ &gt; pool_;

public:
    template&lt;class T&gt;
    bool addItem(std::string key, T*&amp; object)
    {
        std::multimap&lt; std::string,  itemType_ &gt;::iterator
            i = pool_.find(key);
        if(i != pool_.end())
        {
            object = (T*)(*i).second.object;
            (*i).second.count++;
            return true;
        }
        i = pool_.insert(std::pair&lt;std::string,itemType_&gt;(key, (itemType_){1, NULL}));
        object = (T*)(*i).second.object;
        return false;
    }

    template&lt;class T&gt;
    bool removeItem(std::string key)
    {
        std::multimap&lt; std::string,  itemType_ &gt;::iterator
        i = pool_.find(key);
        if(i != pool_.end())
        {
            if((*i).second.count == 1)
            {
                //important to call the appropriate destructor
                delete ((T*)(*i).second.object);
                pool_.erase(i);
            }
            else
                (*i).second.count--;

            return true;
        }
        return false;
    }
};
</code></pre>

<p>And the test code:</p>

<pre><code>#include "objectPool.h"
class testClass
{
public:
    ~testClass()
    {
        // I should get here at least once
        std::cout &lt;&lt; "I am deleted teehee";
    }
};


    testClass* test;

    objectPool myPool;

    int main () {

        if(!myPool.addItem&lt;testClass&gt;("baba", test))
        {
            test = new testClass;
        }

        myPool.removeItem&lt;testClass&gt;("baba");
    }
</code></pre>

<p>For some reason my test object's destructor does not want to be invoked.
First questcha: Why? Where I am wrong?</p>

<p>The sec.: Should I use auto_ptr instead? (Although I want to avoid using templates...)</p>

<p>The third.: Is there a better(-looking) solution? (with or without using templates)</p>

<p>The fourth.: Is there a way to invoke constructor via void pointer without templates (or without knowing the original type)?</p>

<p>Thanks ahead! :D
And sorry for my terrific english (not my native language, although...)</p>
