Special case of "Delete called on base class that is abstract but has non-virtual destructor"

66 Views Asked by At

This is a minimal reproducable example matching my setup :

(using Qt, but the QList can easily be replaced with any kind of std container I guess)

class DataType
{

};

class DerivedDataType : public DataType
{

};

class OtherDerivedDataType : public DataType
{

};

class AbstractDataContainer
{
public :

    ~AbstractDataContainer() { for(DataType* pData : qAsConst(dataList)) { delete pData; } };
    virtual void insertData() = 0;

protected:

    AbstractDataContainer() {};
    QList<DataType*> dataList;
};

template<class T>
class DataContainer : public AbstractDataContainer
{
public:
    DataContainer() : AbstractDataContainer() {};
    ~DataContainer() {};

    void insertData() {DataType* pData = new T(); dataList.append(pData);}
};

void main(void)
{
    QList<AbstractDataContainer*> listOfAbstractDataContainers;
    listOfAbstractDataContainers.append(new DataContainer<DerivedDataType>());
    listOfAbstractDataContainers.append(new DataContainer<OtherDerivedDataType>());

    for(AbstractDataContainer* pAbstractDataContainer : qAsConst(listOfAbstractDataContainers))
    {
        pAbstractDataContainer->insertData();
    }

     /*... do stuff ... */

    /* Finish : clean up. */
    for(AbstractDataContainer* pAbstractDataContainer : qAsConst(listOfAbstractDataContainers))
    {
        delete pAbstractDataContainer;
    }
}

I get the warning "Delete called on base class 'AbstractDataContainer' that is abstract but has non-virtual destructor". Should I be worried ? Am I doing something wrong ? is there an equivalent way of doing what I do without getting this error ?

I only differentiated between AbstractDataContainer and DataContainer because it is not possible to create a list of templated classes without knowing how each element of the container is going to be templated, given that I want every element of the container to have different template arguments, yet still be able to iterate in a generic/polymorphic way over all elements of the container without going into switches for each type of data. At least this is the case with QList and the like in Qt : you cannot write this :

QList<const template<class T> DataContainer> listOfDataContainers;
listOfDataContainers.append(DataContainer<DerivedDataType>);
listOfDataContainers.append(DataContainer<OtherDerivedDataType>);
0

There are 0 best solutions below