Results 1 to 4 of 4

Thread: Extending two class "the same way"

  1. #1
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Extending two class "the same way"

    I have a class hierarchy that contains amongst others
    Qt Code:
    1. class Dialog { ... };
    2. class SpecialDialog : public Dialog { ... };
    To copy to clipboard, switch view to plain text mode 
    I also have a class
    Qt Code:
    1. class ListDialog : public Dialog { ... };
    To copy to clipboard, switch view to plain text mode 

    The functionality of "ListDialog" does not depend on the extensions of SpecialDialog, it would have been possible to have it extend SpecialDialog as well.

    And that is just my question:
    Going from the (already implemented) above classes, is there an easy way to have both a ListDialog and a "SpecialListDialog" that is identical but inherits SpecialDialog instead. (Ideally the solution should allow even further "Dialog"-based classes as subclasses.

    I would hate to copy the source and just change the few references to the base class (mostly in virtual methods). I also don't like too many implementation details in headers (otherwise I'd make a template)...

    I am aware that I could put the complete implementation into the header and make the class a template and the base class a template parameter. (I am looking for a different solution, if possible.)
    Another idea would be to try to put the extension of ListDialog into a separate class and use multiple inheritance. But how would you override the virtual functions, then? (Yes, Dialog does contain virtual methods which are overridden in ListDialog..)

    Best regards and thank you for your ideas.

    (A similar problem is when you want to extend two different (existing) Qt widgets by common functionality...)

  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: Extending two class "the same way"

    To me it seems what you want is more composition than generalization. Basically what you can do is to design interfaces describing each of the "additions" to the Dialog class and then make the "subclasses" implement them. This doesn't have to be through inheritance, the most trivial solution would be to use aforementioned composition.
    Qt Code:
    1. class A {
    2. public:
    3. virtual void x() = 0;
    4. };
    5. class B {
    6. public:
    7. virtual void y() = 0;
    8. };
    9.  
    10. class C {
    11. public:
    12. const A* a() const { return &m_AImpl; }
    13. const B* b() const { return &m_BImpl; }
    14. private:
    15. AImpl m_AImpl; // where AImpl inherits A
    16. BImpl m_BImpl; // where BImpl inherits B
    17. };
    To copy to clipboard, switch view to plain text mode 

    This is more or less what is done in Qt Creator and is called aggregation there.

    What Creator uses is a kind of dynamic aggregations where you can add objects to the "mother-object" on demand. Unfortunately I can't find any articles about such a feature right now. In general you can query an object for an interface and if it has sub-object implementing it, they will be returned.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    caduel (22nd July 2009)

  4. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Extending two class "the same way"

    Thank you, I will think about that.
    The only annoying thing is that this are not "pure" additions, but the ListDialog does reimplement several virtual methods. It is not obvous how to best deal with several such additions as in your example that all might reimplement a, say, keyHandler() to handle some keys...

  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: Extending two class "the same way"

    Quote Originally Posted by caduel View Post
    The only annoying thing is that this are not "pure" additions, but the ListDialog does reimplement several virtual methods.
    Those virtual methods may be an object in the base class as well.

    It is not obvous how to best deal with several such additions as in your example that all might reimplement a, say, keyHandler() to handle some keys...
    Very easy. The base class asks the current object if it implements this particular interface and if so, it delegates the call to it.

    Qt Code:
    1. class Base {
    2. public:
    3. virtual void keyHandler(int x){
    4. // the method doesn't even need to be virtual (which maintains binary compatibility by the way)
    5. KeyHandlerIface *kh = objectFor<KeyHandlerIface*>(this); // ask "this" aggregate to return "KeyHandlerIface" object
    6. if(kh){
    7. kh->handleKey(x);
    8. } else {
    9. //...
    10. }
    11. }
    12. };
    To copy to clipboard, switch view to plain text mode 

    Of course you need to build upon this, objectFor() needs to be implemented using templates and/or qobject_cast.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 0
    Last Post: 3rd January 2009, 05:26
  2. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 07:57
  3. Replies: 2
    Last Post: 4th May 2006, 19:17

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.