Results 1 to 7 of 7

Thread: blocks, storage and objects

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default blocks, storage and objects

    Hello, I have this problem. I read the storage for a block (and thus for all "the things" inside it) are allocate after "{" of the block. Then I found this code below (thinking in c++ page 292 webEdition) that explain how to don't use jump and case (in such case). I uderstand:
    1. the goto can't be there (otherwise I allocate storage but I could "not create" and initialize my1
    2. Why is it ok? ( there is possible to skip My my2; )
    3. With or without "case 2" My my3 are always skipped (then?)

    I hope you'll understanding my troubles; thanks.
    Qt Code:
    1. class My {
    2. public:
    3. My();
    4. };
    5.  
    6. My::My() {}
    7.  
    8. void p292 (int i) {
    9. if (i < 10) {
    10. //goto jump1; // I understand
    11. }
    12. My my1;
    13. jump1:
    14. switch (i) {
    15. case 10:
    16. My my2; // I don't understand
    17. break;
    18. //case 2: //with this it doesn't compile
    19. My my3; // (I don't understand)^2
    20. break;
    21. };
    22. }
    To copy to clipboard, switch view to plain text mode 
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: blocks, storage and objects

    To allocate local variables within a switch/case block you need to use brackets :

    Qt Code:
    1. switch ( i )
    2. {
    3. case 0 :
    4. {
    5. bool compilerError = false;
    6. break;
    7. }
    8.  
    9. default :
    10. bool compilerError = true;
    11. break;
    12. }
    To copy to clipboard, switch view to plain text mode 
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: blocks, storage and objects

    the question wasn't that. (in fact compiler gets errors like " initialization is skipped by 'jump/case 2'....
    Regards

  4. #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: blocks, storage and objects

    You can rewrite the switch as:
    Qt Code:
    1. {
    2. if(i==10) goto _l10; else if(i==2) goto _l2; else goto _lafter;
    3. _l10:
    4. My my2;
    5. goto _lafter;
    6. _l2:
    7. My my3;
    8. goto _lafter;
    9. _lafter:
    10. }
    To copy to clipboard, switch view to plain text mode 

    It should become clear now why some constructions are invalid and others not.

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: blocks, storage and objects

    sorry but isn't clear what am I was wondering in post#1...
    Regards

  6. #6
    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: blocks, storage and objects

    Compare your code and mine. If you look at my code after reading what you wrote regarding "goto", you should understand why compiler denies such "case" constructions.

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: blocks, storage and objects

    Quote Originally Posted by wysota View Post
    Compare your code and mine. If you look at my code after reading what you wrote regarding "goto", you should understand why compiler denies such "case" constructions.
    Sorry please....
    (refer post#1) "jump" gets only a "warning". The presence of "case 2" gets an "error" (reason?). Then: compiler (with case 2) says: "initialization of my2 is skipped by case 2". I say: if i was 2, then "My my2" could be skipped.
    1. why if I comment line "case 2" it don't get error? (running program, could be skipped My my2 (as before) and ALWAYS "MY my3" will be skipped.
    2. Anyway: why compiler doens't get even a message as "initialization of my3 is skipped by case 10" ?
    Maybe I don't know when is the initialization (or similar)....
    Regards

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.