PDA

View Full Version : Subclass QList<T>



Seishin
23rd April 2013, 21:42
Hi,

I want to make a subclass of QList for any type T.

Since I'm not very familiar with templates inheriting, I try to make a mirror copy of QList first.

This is my my_list.h header file


template <class T>
class MyList : public QList<T>
{
public:
MyList() : QList() {}
~MyList() : ~QList() {}
};


I try to use MyList as a QList in the main program, but the code cannot be compiled.
The error is :


error C2059: syntax error : '~' while compiling class template member function 'MyList<T>::~MyList(void)' with [ T = MyObject *]
see reference to class template instantiation 'MyList<T>' being compiled with [ T = MyObject *]


How should I fix the error to create a template works exactly as QList?
Thanks in advance.

ChrisW67
23rd April 2013, 22:12
I want to make a subclass of QList for any type T.
QList already is list of arbitrary type T. It already is a template class.

Santosh Reddy
23rd April 2013, 22:20
Base class destructor should not be called (it is internally called implicitly)



//~MyList() : ~QList() {}
~MyList() {}

Seishin
23rd April 2013, 22:55
QList already is list of arbitrary type T. It already is a template class.

You are right. But I want to override some function of QList later, so I try to make a simple copy subclass first.

ChrisW67
24th April 2013, 00:22
QList is not designed to be inherited and have its behaviours overridden: it has no virtual functions. Its destructor is not virtual so polymorphic pointers (if you were intending to use them) will also be dangerous. You could add behaviours like QStringList does.

You usually better off either using QList<T> as-is or incorporating it by composition rather than inheritance.