I have the following piece of code (from Koening & Moo Accelerated C++ page 255) that defines a generic handle class Handle. Handle is used to manage the memory of objects:
#include <iostream>
#include <stdexcept>
///Handle
template <class T>
class Handle
{
  public:
    Handle() : p(0) {}
    Handle &operator=(const Handle &);
    T *operator->() const;
    ~Handle() { delete p; }
    Handle(T *t) : p(t) {}
  private:
    T *p;
};
template <class T>
Handle<T> &Handle<T>::operator=(const Handle &rhs)
{
    if (&rhs != this)
    {
        delete p;
        p = rhs.p ? rhs.p->clone() : 0;
    }
    return *this;
};
template <class T>
T *Handle<T>::operator->() const
{
    if (p)
        return p;
    throw std::runtime_error("error");
};
class test_classA
{
    friend class Handle<test_classA>;
  private:
    virtual test_classA *clone() const { return new test_classA; }
  public:
    virtual void run() { std::cout << "HiA"; }
};
class test_classB : public test_classA
{
  private:
    virtual test_classB *clone() const { return new test_classB; }
  public:
    virtual void run() { std::cout << "HiB"; }
};
int main()
{
    Handle<test_classA> h;
    h = new test_classA;
    h->run();
    return 0;
}
When I compile this using g++ -o main main.cpp -Wall I get the warning:
warning: deleting object of polymorphic class type ‘test_classA’ which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]
     ~Handle() { delete p; }
I don't quite understand the warning. The handle class automatically deletes the pointer *p in the destructor regardless of its type, so where is the potential pitfall?
 
     
     
     
     
    