Results 1 to 13 of 13

Thread: Polymorphism with static calls

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #10
    Join Date
    Jan 2006
    Posts
    105
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: Polymorphism with static calls

    I want classes that have some kind of static settings, take the code example below:
    Qt Code:
    1. #include <QVariant>
    2. #include <QMap>
    3. #include <QDebug>
    4.  
    5. class A {
    6. public:
    7. static QVariant getSetting(QString s) {
    8. return getSettings()[s];
    9. }
    10. protected:
    11. static QMap<QString, QVariant> getSettings() {
    12. QMap<QString, QVariant> settings;
    13. settings["foo"] = "bar";
    14. settings["fooo"] = "baar";
    15. return settings;
    16. }
    17. };
    18. class B : public A {
    19. static QMap<QString, QVariant> getSettings() {
    20. QMap<QString, QVariant> settings = A::getSettings();
    21. settings["foo"] = "bar1";
    22. return settings;
    23. }
    24. };
    25.  
    26. int main(char** /*argv*/, int /*argc*/) {
    27. qDebug() << A::getSetting("foo");
    28. qDebug() << A::getSetting("fooo");
    29. qDebug() << B::getSetting("foo"); //would like "bar1" here
    30. qDebug() << B::getSetting("fooo");
    31. return 0;
    32. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by niko; 8th November 2007 at 13:50.

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 11:12
  2. Accessing to a static variable from the same class
    By xgoan in forum General Programming
    Replies: 6
    Last Post: 5th March 2007, 11:50

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
  •  
Qt is a trademark of The Qt Company.