Results 1 to 4 of 4

Thread: array + template ?

  1. #1
    Join Date
    Jan 2006
    Location
    Maui, Hawaii
    Posts
    120
    Thanks
    65
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default array + template ?

    Hello!

    I work in a lemonade factory. We have data servers in our vats to tell us things like: pH level, sugar level, etc. Each vat has a separate data structure it passes to the data client GUI which I'm writing.

    I have a bunch of pointers - one for each vat - to a lemonade template class, and I want to get them in an array or list somehow. The problem is that they are all different types.

    Currently, they look like this:
    Qt Code:
    1. LlcsLemonadeDataClient<YELLOW_LEM> *yellow_dc_p;
    2. LlcsLemonadeDataClient<PINK_LEM> *pink_dc_p;
    3. LlcsLemonadeDataClient<MELONADE> *melon_dc_p;
    To copy to clipboard, switch view to plain text mode 

    Is there any way I could do something like:
    LlcsLemonadeDataClient <?> *lemonade_list[10];
    ?

    My goal is to iterate through all the different lemonade data clients I have and set the pointers to NULL. And similar tasks. Currently I have to name every lemonade type in the factory and manually set the pointer to NULL:
    Qt Code:
    1. yellow_dc_p = NULL;
    2. pink_dc_p = NULL;
    3. ...
    To copy to clipboard, switch view to plain text mode 

    Any ideas? Is there an STL or Qt container approach?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: array + template ?

    Wrap those types into classes, like so:

    Qt Code:
    1. struct common{
    2.  
    3. };
    4.  
    5. struct yellow : public common, YELLOW_LEM {
    6.  
    7. };
    8.  
    9. struct pink : public common, PINK_LEM {
    10.  
    11. };
    12.  
    13. struct melon : publik common, MELONADE {
    14.  
    15. };
    To copy to clipboard, switch view to plain text mode 

    And then:

    Qt Code:
    1. std::list<common*> list;
    To copy to clipboard, switch view to plain text mode 

    Or simply:

    Qt Code:
    1. union { YELLOW_LEM *yellow; PINK_LEM *pink; MELONADE *melon; } lemonades;
    2. std::list<lemonades> list;
    To copy to clipboard, switch view to plain text mode 

    You'll have to cast to appropriate classes anyway, so both of this approaches are limited. You'd have to create your own set of classes and wrap each method of each of those pointers directly into a class, so that they share a common interface and have ability for run time type information.

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

    mhoover (30th March 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Maui, Hawaii
    Posts
    120
    Thanks
    65
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: array + template ?

    Thanks again, Wysota.

    Unfortunately it is as you say: a bit limited.

    If I don't end up going with it, at least I learned a thing or two!


  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: array + template ?

    The thing you SHOULD do is to wrap all those classes (structs?) you get pointers to in real classes, sharing the common interface, for example:

    Qt Code:
    1. struct Lemonade {
    2. virtual string taste()=0;
    3. virtual string colour()=0;
    4. //...
    5. };
    6.  
    7. class YellowLemonade : public Lemonade {
    8. public:
    9. YellowLemonade(YELLOW_LEM *l){ m_ptr = l;}
    10. string taste(){ return m_ptr->taste(); } // common
    11. string colour(){ return "yellow"; } // common
    12. string yellowshade() { return m_ptr->shade(); } // yellow only
    13. private:
    14. YELLOW_LEM *m_ptr;
    15. };
    16.  
    17. class PinkLemonade : public Lemonade {
    18. public:
    19. PinkLemonade(PINK_LEM *l){ m_ptr = l;}
    20. string taste(){ return m_ptr->taste(); } // common
    21. string colour(){ return "pink"; } // common
    22. string pinkintensity(){ return m_ptr->intensity(); } // pink only
    23. private:
    24. PINK_LEM *m_ptr;
    25. };
    To copy to clipboard, switch view to plain text mode 
    etc.

  6. The following user says thank you to wysota for this useful post:

    mhoover (30th March 2006)

Similar Threads

  1. Replies: 2
    Last Post: 7th July 2008, 09:44
  2. Template classes and abstraction of type
    By Raistlin in forum General Programming
    Replies: 15
    Last Post: 1st April 2008, 11:18
  3. Problem in converting QString to QChar array?
    By KaKa in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2007, 01:38
  4. how to write a template function in a form..
    By nass in forum General Programming
    Replies: 7
    Last Post: 18th December 2006, 20:36
  5. problem using template
    By mickey in forum General Programming
    Replies: 6
    Last Post: 18th November 2006, 16:57

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.