Results 1 to 5 of 5

Thread: Subclass QList<T>

  1. #1
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Subclass QList<T>

    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
    Qt Code:
    1. template <class T>
    2. class MyList : public QList<T>
    3. {
    4. public:
    5. MyList() : QList() {}
    6. ~MyList() : ~QList() {}
    7. };
    To copy to clipboard, switch view to plain text mode 

    I try to use MyList as a QList in the main program, but the code cannot be compiled.
    The error is :
    Qt Code:
    1. error C2059: syntax error : '~' while compiling class template member function 'MyList<T>::~MyList(void)' with [ T = MyObject *]
    2. see reference to class template instantiation 'MyList<T>' being compiled with [ T = MyObject *]
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Subclass QList<T>

    Quote Originally Posted by Seishin View Post
    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.

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Subclass QList<T>

    Base class destructor should not be called (it is internally called implicitly)

    Qt Code:
    1. //~MyList() : ~QList() {}
    2. ~MyList() {}
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  4. The following user says thank you to Santosh Reddy for this useful post:

    Seishin (23rd April 2013)

  5. #4
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Subclass QList<T>

    Quote Originally Posted by ChrisW67 View Post
    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.

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Subclass QList<T>

    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.

Similar Threads

  1. Replies: 2
    Last Post: 15th April 2013, 06:33
  2. Qlist<QLabel *> in Qlist<QAction*>
    By Naahmi in forum Qt Programming
    Replies: 1
    Last Post: 9th September 2011, 08:53
  3. QObject subclass in a QList
    By plan_rich in forum Newbie
    Replies: 3
    Last Post: 21st September 2010, 15:51
  4. Replies: 4
    Last Post: 20th August 2010, 13:54
  5. Replies: 8
    Last Post: 12th February 2010, 02:41

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.