Results 1 to 4 of 4

Thread: QMap value type with virtual destructor

  1. #1
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default QMap value type with virtual destructor

    Hello,

    I solved a problem that troubled me for a while and now I am interested in getting more insight into what has been happening here..

    I have a custom data type that I want to store as a value in QMap.
    Qt Code:
    1. QMap<uint, CustomType>
    To copy to clipboard, switch view to plain text mode 

    My CustomType looks like:

    Qt Code:
    1. class CustomType
    2. {
    3. public:
    4. CustomType() { m_objectRef = nullptr; m_slotName = nullptr; }
    5. CustomType(QObject* obj, const char* slotName) { m_objectRef = obj; m_slotName = slotName; }
    6. virtual ~CustomType() {}
    7.  
    8. // Accessors
    9. QObject* GetObjectName() { return m_objectRef; }
    10. const char* GetSlotName() { return m_slotName; }
    11.  
    12. private:
    13. QObject* m_objectRef;
    14. const char* m_slotName;
    15. };
    To copy to clipboard, switch view to plain text mode 

    I get a compilation error saying:
    Qt Code:
    1. undefined reference to `_imp___ZTV10CustomType'
    To copy to clipboard, switch view to plain text mode 

    Solution:
    Remove virtual keyword from the destructor.

    My question:
    Why can't I have a virtual destructor here?

    Thank you!

    Regards
    Vikram

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QMap value type with virtual destructor

    Do not store or pass polymorphic types by value, pass by reference, pointer or most preferably wrap your objects in smart pointers. Passing polymorphic objects by value can cause slicing.
    Unrelated, but using constructor initializer lists instead of assigning in constructor body is a good habit.

  3. The following user says thank you to stampede for this useful post:

    Vikram.Saralaya (25th November 2015)

  4. #3
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMap value type with virtual destructor

    Quote Originally Posted by Vikram.Saralaya View Post
    Solution:
    Remove virtual keyword from the destructor.

    My question:
    Why can't I have a virtual destructor here?
    I tried your code actually it is getting complied with and without virtual.
    Thanks :-)

  5. #4
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMap value type with virtual destructor

    Quote Originally Posted by prasad_N View Post
    I tried your code actually it is getting complied with and without virtual.
    I can confirm that I can easily reproduce that problem on my mingw-32bit compiler. Strange.. well I will stop worrying about it for now. Thanks for trying

Similar Threads

  1. QMap with pointer to custom type
    By Eos Pengwern in forum Qt Programming
    Replies: 2
    Last Post: 20th November 2014, 07:35
  2. Replies: 4
    Last Post: 5th April 2012, 12:38
  3. Replies: 12
    Last Post: 8th July 2011, 05:23
  4. Q_PROPERTY of type QMap
    By eclarkso in forum Qt Programming
    Replies: 11
    Last Post: 30th October 2009, 18:43
  5. QMap destructor bug
    By Ruud in forum Qt Programming
    Replies: 6
    Last Post: 8th April 2006, 09:08

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.