Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is there a "new" style for casting?
Forum Updated to NodeBB v4.3 + New Features

Is there a "new" style for casting?

Scheduled Pinned Locked Moved Solved General and Desktop
46 Posts 9 Posters 32.9k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AbrarA Abrar

    @saber as @J-Hilk and @jsulm said
    int x = int(screensize().width() * .8);
    int y = int(screensize().height() * .7);

    @J-Hilk and @jsulm make it very easy to me to understand.

    jsulmJ Online
    jsulmJ Online
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #13

    @Abrar said in Is there a "new" style for casting?:

    int x = int(screensize().width() * .8);
    int y = int(screensize().height() * .7);

    This would bring back the warning about old style cast.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    AbrarA 1 Reply Last reply
    1
    • jsulmJ jsulm

      @Abrar said in Is there a "new" style for casting?:

      int x = int(screensize().width() * .8);
      int y = int(screensize().height() * .7);

      This would bring back the warning about old style cast.

      AbrarA Offline
      AbrarA Offline
      Abrar
      wrote on last edited by
      #14

      @jsulm is it look like this
      int x = static_cast<int>(screensize().width() * .8);
      int y = static_cast<int>(screensize().height() * .7);

      If not tell me the difference between new cast and old...plz

      jsulmJ 1 Reply Last reply
      1
      • AbrarA Abrar

        @jsulm is it look like this
        int x = static_cast<int>(screensize().width() * .8);
        int y = static_cast<int>(screensize().height() * .7);

        If not tell me the difference between new cast and old...plz

        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #15

        @Abrar said in Is there a "new" style for casting?:

        static_cast<int>(screensize().width() * .8)

        Yes, this is C++ type cast.

        (int)(someVariabl);
        

        Old C style cast which should not be used in C++.
        See https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        J.HilkJ AbrarA 2 Replies Last reply
        1
        • jsulmJ jsulm

          @Abrar said in Is there a "new" style for casting?:

          static_cast<int>(screensize().width() * .8)

          Yes, this is C++ type cast.

          (int)(someVariabl);
          

          Old C style cast which should not be used in C++.
          See https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used

          J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #16

          @jsulm
          technically,

          int(someVariabl);

          is a valid "conversion", as it's a functional notation


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          jsulmJ 1 Reply Last reply
          2
          • jsulmJ jsulm

            @Abrar said in Is there a "new" style for casting?:

            static_cast<int>(screensize().width() * .8)

            Yes, this is C++ type cast.

            (int)(someVariabl);
            

            Old C style cast which should not be used in C++.
            See https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used

            AbrarA Offline
            AbrarA Offline
            Abrar
            wrote on last edited by
            #17

            @jsulm ok now understand the difference.
            Thanks...

            1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @jsulm
              technically,

              int(someVariabl);

              is a valid "conversion", as it's a functional notation

              jsulmJ Online
              jsulmJ Online
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #18

              @J.Hilk Both are C-style casts

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              J.HilkJ sierdzioS 2 Replies Last reply
              0
              • S Offline
                S Offline
                saber
                wrote on last edited by saber
                #19

                guys what a
                @saber = static_cast<learn>( @jsulm and @J-Hilk ; (i think)

                thanks.learned alot.

                AbrarA 1 Reply Last reply
                1
                • S saber

                  guys what a
                  @saber = static_cast<learn>( @jsulm and @J-Hilk ; (i think)

                  thanks.learned alot.

                  AbrarA Offline
                  AbrarA Offline
                  Abrar
                  wrote on last edited by
                  #20

                  @saber you wrote the clever statement ever.
                  !!!!

                  1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @J.Hilk Both are C-style casts

                    J.HilkJ Online
                    J.HilkJ Online
                    J.Hilk
                    Moderators
                    wrote on last edited by J.Hilk
                    #21

                    @jsulm thats not what @aha_1980 said earlier, and what I read from my trusted source
                    (http://www.cplusplus.com/doc/tutorial/typecasting/) :-)

                    the new (qtcreator)clang code model seems to agree as well, as int(xy) is not flaged as old style casting.


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    jsulmJ 1 Reply Last reply
                    1
                    • J.HilkJ J.Hilk

                      @jsulm thats not what @aha_1980 said earlier, and what I read from my trusted source
                      (http://www.cplusplus.com/doc/tutorial/typecasting/) :-)

                      the new (qtcreator)clang code model seems to agree as well, as int(xy) is not flaged as old style casting.

                      jsulmJ Online
                      jsulmJ Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #22

                      @J.Hilk Yeah, I think I was wrong on function style cast you mentioned

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        saber
                        wrote on last edited by saber
                        #23

                        i got more warnings to fix.

                        0_1534227012370_t.png

                            switch( m_model->state() ) {
                                case Battery::FullyCharged:
                                    ui->statusEdit->setText( tr( "Full Charged" ) );
                                    ui->timerLblEdit->setText( tr( "Full Charged" ) );
                                    break;
                                case Battery::Discharging:
                                    addSeconds = static_cast<int>(m_model->toEmpty());
                                    addSeconds = rate != 0 && addSeconds == 0 ? ( energy - energyEmpty ) * 4 / rate: addSeconds;
                                    ui->statusEdit->setText( tr( "Discharging" ) );
                                    ui->timerLblEdit->setText( tr( "Discharged in : " ) );
                                    break;
                                case Battery::Charging:
                                    addSeconds = static_cast<int>(m_model->toFull());
                                    addSeconds = rate != 0 && addSeconds == 0 ? ( energyFull - energy ) * 4 / rate : addSeconds;
                                    ui->statusEdit->setText( tr( "Charging" ) );
                                    ui->timerLblEdit->setText( tr( "Charged in : " ) );
                                    break;
                                default:
                                    ui->statusEdit->setText( tr( "No Battery" ) );
                                    ui->timerLblEdit->setText( tr( "No Battery" ) );
                                    break;
                            }
                        
                        jsulmJ 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @J.Hilk Both are C-style casts

                          sierdzioS Offline
                          sierdzioS Offline
                          sierdzio
                          Moderators
                          wrote on last edited by
                          #24

                          @jsulm said in Is there a "new" style for casting?:

                          @J.Hilk Both are C-style casts

                          Yep but clang does not complain about int(something) while it does complain about (int) something. Just sayin' :-)

                          (Z(:^

                          1 Reply Last reply
                          0
                          • S saber

                            i got more warnings to fix.

                            0_1534227012370_t.png

                                switch( m_model->state() ) {
                                    case Battery::FullyCharged:
                                        ui->statusEdit->setText( tr( "Full Charged" ) );
                                        ui->timerLblEdit->setText( tr( "Full Charged" ) );
                                        break;
                                    case Battery::Discharging:
                                        addSeconds = static_cast<int>(m_model->toEmpty());
                                        addSeconds = rate != 0 && addSeconds == 0 ? ( energy - energyEmpty ) * 4 / rate: addSeconds;
                                        ui->statusEdit->setText( tr( "Discharging" ) );
                                        ui->timerLblEdit->setText( tr( "Discharged in : " ) );
                                        break;
                                    case Battery::Charging:
                                        addSeconds = static_cast<int>(m_model->toFull());
                                        addSeconds = rate != 0 && addSeconds == 0 ? ( energyFull - energy ) * 4 / rate : addSeconds;
                                        ui->statusEdit->setText( tr( "Charging" ) );
                                        ui->timerLblEdit->setText( tr( "Charged in : " ) );
                                        break;
                                    default:
                                        ui->statusEdit->setText( tr( "No Battery" ) );
                                        ui->timerLblEdit->setText( tr( "No Battery" ) );
                                        break;
                                }
                            
                            jsulmJ Online
                            jsulmJ Online
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #25

                            @saber said in Is there a "new" style for casting?:

                            i got more warnings to fix.

                            So, fix them

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            S 1 Reply Last reply
                            1
                            • jsulmJ jsulm

                              @saber said in Is there a "new" style for casting?:

                              i got more warnings to fix.

                              So, fix them

                              S Offline
                              S Offline
                              saber
                              wrote on last edited by
                              #26

                              @jsulm
                              sorry .
                              i don't think those are casting.and i am not understanding what is the fix.

                              jsulmJ 1 Reply Last reply
                              0
                              • S saber

                                @jsulm
                                sorry .
                                i don't think those are casting.and i am not understanding what is the fix.

                                jsulmJ Online
                                jsulmJ Online
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by jsulm
                                #27

                                @saber The first warning says already what the problem is: comparing floating point numbers with == and != isn't save because of inaccuracy of such numbers represented in a computer. Depending on what you really want you should cast to int or use <, > for comparisions.
                                For the second warning take a look at https://softwareengineering.stackexchange.com/questions/179269/why-does-clang-llvm-warn-me-about-using-default-in-a-switch-statement-where-all
                                In general, you should try to find out what the problem is by yourself first before going to a forum and asking. Or do you want to ask for each and every warning you get? I mean, I'm happy to help, but sometimes I have the impression that people don't even try to find an answer by themselves.

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                S 1 Reply Last reply
                                2
                                • jsulmJ jsulm

                                  @saber The first warning says already what the problem is: comparing floating point numbers with == and != isn't save because of inaccuracy of such numbers represented in a computer. Depending on what you really want you should cast to int or use <, > for comparisions.
                                  For the second warning take a look at https://softwareengineering.stackexchange.com/questions/179269/why-does-clang-llvm-warn-me-about-using-default-in-a-switch-statement-where-all
                                  In general, you should try to find out what the problem is by yourself first before going to a forum and asking. Or do you want to ask for each and every warning you get? I mean, I'm happy to help, but sometimes I have the impression that people don't even try to find an answer by themselves.

                                  S Offline
                                  S Offline
                                  saber
                                  wrote on last edited by
                                  #28

                                  @jsulm
                                  i understand all the warnings but problem is i don't know how to fix those .

                                  i know this issues are small and silly to ask to help.but i have no other way. my app's co-developer is in off-line. i am a ui designer and newbie c++ developer .

                                  thanks.

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • S saber

                                    @jsulm
                                    i understand all the warnings but problem is i don't know how to fix those .

                                    i know this issues are small and silly to ask to help.but i have no other way. my app's co-developer is in off-line. i am a ui designer and newbie c++ developer .

                                    thanks.

                                    jsulmJ Online
                                    jsulmJ Online
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by jsulm
                                    #29

                                    @saber I already suggested what to change and provided a link. And you can round the floating point numbers to int to get rid of this message. And you can use Google.
                                    Also you should think about whether you need floating point numbers at all...

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    S 1 Reply Last reply
                                    0
                                    • jsulmJ jsulm

                                      @saber I already suggested what to change and provided a link. And you can round the floating point numbers to int to get rid of this message. And you can use Google.
                                      Also you should think about whether you need floating point numbers at all...

                                      S Offline
                                      S Offline
                                      saber
                                      wrote on last edited by
                                      #30

                                      @jsulm
                                      yes , it seems fixed.

                                          switch( m_model->state() ) {
                                              case Battery::FullyCharged:
                                                  ui->statusEdit->setText( tr( "Full Charged" ) );
                                                  ui->timerLblEdit->setText( tr( "Full Charged" ) );
                                                  break;
                                              case Battery::Discharging:
                                                  addSeconds = static_cast<int>(m_model->toEmpty());
                                                  addSeconds = static_cast<int>(rate) != 0 && addSeconds == 0 ? static_cast<int>(( energy - energyEmpty ) * 4 / rate) : addSeconds;
                                                  ui->statusEdit->setText( tr( "Discharging" ) );
                                                  ui->timerLblEdit->setText( tr( "Discharged in : " ) );
                                                  break;
                                              case Battery::Charging:
                                                  addSeconds = static_cast<int>(m_model->toFull());
                                                  addSeconds = static_cast<int>(rate) != 0 && addSeconds == 0 ? static_cast<int>(( energyFull - energy ) * 4 / rate) : addSeconds;
                                                  ui->statusEdit->setText( tr( "Charging" ) );
                                                  ui->timerLblEdit->setText( tr( "Charged in : " ) );
                                                  break;
                                              default:
                                                  ui->statusEdit->setText( tr( "No Battery" ) );
                                                  ui->timerLblEdit->setText( tr( "No Battery" ) );
                                                  break;
                                          }
                                      

                                      as for the default , i read the post and understand that i can't do anything as the library i am using there is no eum like "no battery" . so it needs the default case.

                                      jsulmJ 1 Reply Last reply
                                      0
                                      • S saber

                                        @jsulm
                                        yes , it seems fixed.

                                            switch( m_model->state() ) {
                                                case Battery::FullyCharged:
                                                    ui->statusEdit->setText( tr( "Full Charged" ) );
                                                    ui->timerLblEdit->setText( tr( "Full Charged" ) );
                                                    break;
                                                case Battery::Discharging:
                                                    addSeconds = static_cast<int>(m_model->toEmpty());
                                                    addSeconds = static_cast<int>(rate) != 0 && addSeconds == 0 ? static_cast<int>(( energy - energyEmpty ) * 4 / rate) : addSeconds;
                                                    ui->statusEdit->setText( tr( "Discharging" ) );
                                                    ui->timerLblEdit->setText( tr( "Discharged in : " ) );
                                                    break;
                                                case Battery::Charging:
                                                    addSeconds = static_cast<int>(m_model->toFull());
                                                    addSeconds = static_cast<int>(rate) != 0 && addSeconds == 0 ? static_cast<int>(( energyFull - energy ) * 4 / rate) : addSeconds;
                                                    ui->statusEdit->setText( tr( "Charging" ) );
                                                    ui->timerLblEdit->setText( tr( "Charged in : " ) );
                                                    break;
                                                default:
                                                    ui->statusEdit->setText( tr( "No Battery" ) );
                                                    ui->timerLblEdit->setText( tr( "No Battery" ) );
                                                    break;
                                            }
                                        

                                        as for the default , i read the post and understand that i can't do anything as the library i am using there is no eum like "no battery" . so it needs the default case.

                                        jsulmJ Online
                                        jsulmJ Online
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #31

                                        @saber You can: remove the default. If then a new value is added to the enum you will get a warning from compiler that this new value is not handled.

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply
                                        0
                                        • S Offline
                                          S Offline
                                          saber
                                          wrote on last edited by saber
                                          #32

                                          for now i will leave as it is and inform the library author to add that enm.

                                          i got more warning to fix.

                                          0_1534231505882_aw.png

                                          i am getting this in all my ui based header file .
                                          but the app compiles fine.

                                          sierdzioS JonBJ jsulmJ 3 Replies Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved