Results 1 to 13 of 13

Thread: Polymorphism with static calls

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

    Default Polymorphism with static calls

    Hello,

    Is it possible to get when calling B::test() 2 printed out?
    Qt Code:
    1. class A {
    2. private:
    3. static int i;
    4. public:
    5. static void test() {
    6. cout << i << endl;
    7. }
    8. };
    9.  
    10. class B : public A {
    11. static int i;
    12. };
    13.  
    14. int A::i = 1;
    15. int B::i = 2;
    16.  
    17. int main(char** argv, int argc) {
    18. A::test(); //prints 1
    19. B::test(); //prints 1
    20. return 0;
    21. }
    To copy to clipboard, switch view to plain text mode 

    thanks,
    niko

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Polymorphism with static calls

    you are shadowing (redeclaring) 'i' in your B class.
    Remove that and it should do what you want.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Polymorphism with static calls

    And make i protected in A and provide a public getter and setter, in A or B. This way you will be able to set it from B too.

  4. #4
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Polymorphism with static calls

    sorry, but I can't get this working.

    i made i now protected - doesn't help:
    Qt Code:
    1. class A {
    2. protected:
    3. static int i;
    4. public:
    5. static void test() {
    6. cout << i << endl;
    7. }
    8. };
    9.  
    10. class B : public A {
    11. protected:
    12. static int i;
    13. };
    14.  
    15. int A::i = 1;
    16. int B::i = 2;
    17.  
    18. int main(char** argv, int argc) {
    19. A::test(); //prints 1
    20. B::test(); //prints 1
    21. return 0;
    22. }
    To copy to clipboard, switch view to plain text mode 

    And make i protected in A and provide a public getter and setter, in A or B. This way you will be able to set it from B too.
    I don't understand what you mean - I do have static calls only!

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Polymorphism with static calls

    Qt Code:
    1. class B : public A {
    2. //protected:
    3.  
    4. // static int i; //you don't want that
    5. };
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Polymorphism with static calls

    I did try that too, it results in this compiler error:
    Qt Code:
    1. main.cpp:19: error: ISO C++ does not permit 'A::i' to be defined as 'B::i'
    2. main.cpp:19: error: redefinition of 'int A::i'
    3. main.cpp:18: error: 'int A::i' previously defined here
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Polymorphism with static calls

    because its protected.
    If your want to initialize it in global scope you will have to make it public.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Polymorphism with static calls

    i get exactly the same compiler error
    Qt Code:
    1. class A {
    2. public:
    3. static int i;
    4. static void test() {
    5. cout << i << endl;
    6. }
    7. };
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Polymorphism with static calls

    Well I don't think this is possible (but others may correct me on this), since AFAIK a static member of a class is nothing more then a global variable that exists in the class's namespace.

    So when you initialize your i member for B, you might try B::A::i = 2; (I didn't try this though)
    But that would make no sense, since the 'i' member of A IS the same member of B, there is only ONE instance of it, not one per object!

    What it is you need? I am sure if you explain what it is your are trying to achieve we can help you find a better way than using static members.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    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 12:50.

  11. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Polymorphism with static calls

    Why not use QSettings then?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  12. #12
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Polymorphism with static calls

    these are not user settings - these are static settings that are compiled into the application.

    but thanks for your replys...

  13. #13
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Polymorphism with static calls

    I still fail to understand what it is you are trying to do, and why does it have to be static.
    You can use QSettings to store settings, be it user, or non user settings.
    It does nothing more then store values for keys, just like you are trying to do manually in your code.
    Then you can have either a settings file for each class, or all settings in one file separated by sections or what have you.

    From your code, I don't see the need to use static variales at all, and the fact you want polymorphism speak against it.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 10:12
  2. Accessing to a static variable from the same class
    By xgoan in forum General Programming
    Replies: 6
    Last Post: 5th March 2007, 10: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.