Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. (int() function vs (int) cast - comments please

(int() function vs (int) cast - comments please

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studioquestion
3 Posts 3 Posters 0 Views 1 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.
  • F Offline
    F Offline
    FearlessBurner
    wrote on last edited by
    #1

    Sometimes, it is necessary to change a floating point number to an integer. The usual way of doing this is just to write i = (int)f; However, the C++ compiler also lets one express this desire like this: i = int( f ); which is how functions are normally written, and it is easier to understand in complicated expressions rather than casts. Similar expressions can be produced with other implicit types eg double and float eg dbl = double( i ); Does anyone know when it became possible to write the code like this, as opposed to using casts? Does anyone know if there are any drawbacks or using this approach? All comments are welcome:)

    A I 2 Replies Last reply
    0
    • F FearlessBurner

      Sometimes, it is necessary to change a floating point number to an integer. The usual way of doing this is just to write i = (int)f; However, the C++ compiler also lets one express this desire like this: i = int( f ); which is how functions are normally written, and it is easier to understand in complicated expressions rather than casts. Similar expressions can be produced with other implicit types eg double and float eg dbl = double( i ); Does anyone know when it became possible to write the code like this, as opposed to using casts? Does anyone know if there are any drawbacks or using this approach? All comments are welcome:)

      A Offline
      A Offline
      Alexander M
      wrote on last edited by
      #2

      i had a look at this in the disassembler, didnt find any difference! Don't try it, just do it! ;-)

      1 Reply Last reply
      0
      • F FearlessBurner

        Sometimes, it is necessary to change a floating point number to an integer. The usual way of doing this is just to write i = (int)f; However, the C++ compiler also lets one express this desire like this: i = int( f ); which is how functions are normally written, and it is easier to understand in complicated expressions rather than casts. Similar expressions can be produced with other implicit types eg double and float eg dbl = double( i ); Does anyone know when it became possible to write the code like this, as opposed to using casts? Does anyone know if there are any drawbacks or using this approach? All comments are welcome:)

        I Offline
        I Offline
        Ian Darling
        wrote on last edited by
        #3

        FearlessBurner wrote: _i = (int)f;_ Is a cast. FearlessBurner wrote: _i = int( f );_ Constructs an unnamed (temporary) int, using f as the initialiser value, and assigns/copies the value to i. Your compiler probably optimises this. FearlessBurner wrote: _dbl = double( i );_ Is similar to the previous comment, and constructs a temporary unnamed double. FearlessBurner wrote: Does anyone know when it became possible to write the code like this, as opposed to using casts? Does anyone know if there are any drawbacks or using this approach? It probably became possible to write this when you changed from C to C++. My own recommendation is to cast using the C++ casts, instead of the C casts, so for the example above I'd write: i = static_cast<int> (f); Because it's easier to find casts that use the C++ syntax for casts over the C syntax, and using the correct C++ cast can reduce or highlight potential errors caused by casting - this particularly applies to the other three C++ casts (const_cast, dynamic_cast, and reinterpret_cast) -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups