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.
  • S saber

    i am getting lot of this warning in my code
    0_1534137549626_w2.png

    any fixes?

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #2

    @saber
    take a look at the c++ basic's

    http://www.cplusplus.com/doc/tutorial/typecasting/

    the tl;dr version

    double x = 10.3;
    int y;
    y = int (x); // functional notation
    y = (int) x; // c-like cast notation

    c++ casting

    dynamic_cast <new_type> (expression)
    reinterpret_cast <new_type> (expression)
    static_cast <new_type> (expression)
    const_cast <new_type> (expression)


    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.

    aha_1980A 1 Reply Last reply
    5
    • J.HilkJ J.Hilk

      @saber
      take a look at the c++ basic's

      http://www.cplusplus.com/doc/tutorial/typecasting/

      the tl;dr version

      double x = 10.3;
      int y;
      y = int (x); // functional notation
      y = (int) x; // c-like cast notation

      c++ casting

      dynamic_cast <new_type> (expression)
      reinterpret_cast <new_type> (expression)
      static_cast <new_type> (expression)
      const_cast <new_type> (expression)

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #3

      @J.Hilk said in Is there a "new" style for casting?:

      y = int (x); // functional notation

      Note that this is also a valid C++ style cast, which is still used for numeric types, as it's less noise than the xxx_cast<>() versions.

      Qt has to stay free or it will die.

      J.HilkJ 1 Reply Last reply
      3
      • aha_1980A aha_1980

        @J.Hilk said in Is there a "new" style for casting?:

        y = int (x); // functional notation

        Note that this is also a valid C++ style cast, which is still used for numeric types, as it's less noise than the xxx_cast<>() versions.

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #4

        @aha_1980
        thanks; I Didn't make that clear enough, my bad for simply copy and pasting :-)


        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.

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

          can you modify those warnings lines in "new cast"
          it will make it clear for me.
          thanks.
          @jsulm can you ? please

          // Check available space on destination before we start
              struct statfs info;
              statfs(newPath.toLocal8Bit(), &info);
              if ((qint64) info.f_bavail * info.f_bsize < total) {  // here 
          
                // If it is a cut/move on the same device it doesn't matter
                if (cutList.count()) {
                  qint64 driveSize = (qint64) info.f_bavail*info.f_bsize;  // here 
                  statfs(files.at(0).path().toLocal8Bit(),&info);
          
                  // Same device?
                  if ((qint64) info.f_bavail*info.f_bsize != driveSize) {  // here 
                    emit copyProgressFinished(2, newFiles);
                    return 0;
                  }
                } else {
                  emit copyProgressFinished(2, newFiles);
                  return 0;
                }
              }
          
          
          J.HilkJ jsulmJ 2 Replies Last reply
          0
          • S saber

            can you modify those warnings lines in "new cast"
            it will make it clear for me.
            thanks.
            @jsulm can you ? please

            // Check available space on destination before we start
                struct statfs info;
                statfs(newPath.toLocal8Bit(), &info);
                if ((qint64) info.f_bavail * info.f_bsize < total) {  // here 
            
                  // If it is a cut/move on the same device it doesn't matter
                  if (cutList.count()) {
                    qint64 driveSize = (qint64) info.f_bavail*info.f_bsize;  // here 
                    statfs(files.at(0).path().toLocal8Bit(),&info);
            
                    // Same device?
                    if ((qint64) info.f_bavail*info.f_bsize != driveSize) {  // here 
                      emit copyProgressFinished(2, newFiles);
                      return 0;
                    }
                  } else {
                    emit copyProgressFinished(2, newFiles);
                    return 0;
                  }
                }
            
            
            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by J.Hilk
            #6

            @saber
            Don't post screenshots of your code, makes it difficult to actuallly quote it and modify it.

            0_1534158240928_9e5b4388-8b78-4613-a351-1439e1850d63-image.png


            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.

            S 2 Replies Last reply
            2
            • S saber

              can you modify those warnings lines in "new cast"
              it will make it clear for me.
              thanks.
              @jsulm can you ? please

              // Check available space on destination before we start
                  struct statfs info;
                  statfs(newPath.toLocal8Bit(), &info);
                  if ((qint64) info.f_bavail * info.f_bsize < total) {  // here 
              
                    // If it is a cut/move on the same device it doesn't matter
                    if (cutList.count()) {
                      qint64 driveSize = (qint64) info.f_bavail*info.f_bsize;  // here 
                      statfs(files.at(0).path().toLocal8Bit(),&info);
              
                      // Same device?
                      if ((qint64) info.f_bavail*info.f_bsize != driveSize) {  // here 
                        emit copyProgressFinished(2, newFiles);
                        return 0;
                      }
                    } else {
                      emit copyProgressFinished(2, newFiles);
                      return 0;
                    }
                  }
              
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #7

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

              can you ? please

              I already did in one of your other threads

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

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

                @saber
                Don't post screenshots of your code, makes it difficult to actuallly quote it and modify it.

                0_1534158240928_9e5b4388-8b78-4613-a351-1439e1850d63-image.png

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

                @J.Hilk i did others

                if (cutList.count()) {
                        qint64 driveSize = static_cast<qint64>(info.f_bavail) * (info.f_bsize) ;
                        statfs(files.at(0).path().toLocal8Bit(),&info);
                
                        // Same device?
                        if (static_cast<qint64>(info.f_bavail) * (info.f_bsize)  != driveSize) {
                          emit copyProgressFinished(2, newFiles);
                          return 0;
                        }
                
                1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @saber
                  Don't post screenshots of your code, makes it difficult to actuallly quote it and modify it.

                  0_1534158240928_9e5b4388-8b78-4613-a351-1439e1850d63-image.png

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

                  @J.Hilk i got other warnings like

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

                  and warning is

                  /home/shaber/Desktop/Folder/work3/coreApps/app/corefm/corefm.cpp:29: warning: implicit conversion turns floating-point number into integer: 'double' to 'int'
                  
                  jsulmJ J.HilkJ AbrarA 3 Replies Last reply
                  0
                  • S saber

                    @J.Hilk i got other warnings like

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

                    and warning is

                    /home/shaber/Desktop/Folder/work3/coreApps/app/corefm/corefm.cpp:29: warning: implicit conversion turns floating-point number into integer: 'double' to 'int'
                    
                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by jsulm
                    #10

                    @saber You are multiplying an int with a double and assign the result to an int, this is dangerous.
                    To get rid of the warning you can cast the result of the multiplication to int or (even better) think about what you're doing and whether rounding in a defined way would be better.

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

                    1 Reply Last reply
                    1
                    • S saber

                      @J.Hilk i got other warnings like

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

                      and warning is

                      /home/shaber/Desktop/Folder/work3/coreApps/app/corefm/corefm.cpp:29: warning: implicit conversion turns floating-point number into integer: 'double' to 'int'
                      
                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #11

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

                      @J.Hilk i got other warnings like

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

                      and warning is

                      /home/shaber/Desktop/Folder/work3/coreApps/app/corefm/corefm.cpp:29: warning: implicit conversion turns floating-point number into integer: 'double' to 'int'
                      

                      of course,
                      this breaks down to

                      int = int * double;
                      int = double <- precision loss, truncated after the decimal point

                      this is an implicit conversion of double to int, therefore cast it alsoif thats intendet behaviour:

                      int x = static_cast<int>(screensize().width()  * .8);
                      

                      or round it

                      int x = qRound(screensize().width()  * .8);
                      

                      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.

                      1 Reply Last reply
                      1
                      • S saber

                        @J.Hilk i got other warnings like

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

                        and warning is

                        /home/shaber/Desktop/Folder/work3/coreApps/app/corefm/corefm.cpp:29: warning: implicit conversion turns floating-point number into integer: 'double' to 'int'
                        
                        AbrarA Offline
                        AbrarA Offline
                        Abrar
                        wrote on last edited by Abrar
                        #12

                        @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 1 Reply Last reply
                        1
                        • 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 Offline
                          jsulmJ Offline
                          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 Offline
                              jsulmJ Offline
                              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 Offline
                                J.HilkJ Offline
                                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 Offline
                                    jsulmJ Offline
                                    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 Offline
                                          J.HilkJ Offline
                                          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

                                          • Login

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