Results 1 to 4 of 4

Thread: Containers with derived classes? (QMap or QList)

  1. #1
    Join Date
    May 2012
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question Containers with derived classes? (QMap or QList)

    Hi all,

    This is my first post and early experience with Qt so appologies if this is answered elsewhere but i can not find it.

    Here is an example of my problem.

    I have base class 'Fruit' and several derived classes let's say 'Apple' and Bannana'

    I want to store these in a container, for example a QMap.

    So i have created a QMap of Fruits.

    I know if i was writting in c# for example i could call the equivelant of typename or typeid on each item and the cast them back to 'Apple' or 'Bannana' when i read them out.

    However it seems here i have lost all of the properties of Apple and Bannana and am left with just 'Fruit's.
    If i call typename on each item it always returns 'Fruit'

    Am i missing something or can i not create a list of derived classes by creating a list of the base class?

    Hope this makes sense.

    Thanks in advance.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Containers with derived classes? (QMap or QList)

    if you store a list of fruits, then you should only need to treat them as fruits, otherwise your design is wrong (liskov substitution principle, I think).

    You should only need to use virtual methods on Fruit to get specialised behaviour, and should not need to recover the exact type.

    Tell me, how are the available properties of Apple different to that of Banana?


    This is basic C++ and nothing to do with Qt.


    If you make a QList<Fruit>, then you have only Fruit objects. If you make a QList<Fruit*>, then you may have Fruit, Apple and Banana...
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    May 2012
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Containers with derived classes? (QMap or QList)

    Thanks for the reply.

    The problem i have is that i want to store a list of objects, this list is of different object types, some with common properties and some with different properties.
    I then wanted to serialize this data.

    Another application would then deserialize this data and obtain a list of objects. It could then iterate through them and act accordingly according to what type they were.

    I have done this many times in other languages using the approach i mentioned (extending a base class and then creating a list of the base class)

    I am fairly new to c++ but have much experience in other languages.

    Thanks again.

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Containers with derived classes? (QMap or QList)

    then in c++ you must use a container of pointers or references then. You cannot use containers of base types if you want polymorphic behaviour - The container objects will be sliced if you attempt to store specialised objects.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Replies: 2
    Last Post: 18th June 2013, 10:31
  2. QMap QList
    By dare5421 in forum Qt Programming
    Replies: 1
    Last Post: 23rd March 2011, 23:17
  3. Replies: 1
    Last Post: 25th June 2010, 13:17
  4. Replies: 2
    Last Post: 20th September 2007, 12:27
  5. QMap in QList ?
    By npc in forum Newbie
    Replies: 2
    Last Post: 5th February 2007, 11:51

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.