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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Type Casting

Type Casting

Scheduled Pinned Locked Moved C / C++ / MFC
4 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.
  • G Offline
    G Offline
    gothic_coder
    wrote on last edited by
    #1

    Hello All, I am always confused between static, dynamic, and reinterpret casting. I tried to find answers but somehow they all are confusing... Could anyone clears this concept. Thanks.

    C S 2 Replies Last reply
    0
    • G gothic_coder

      Hello All, I am always confused between static, dynamic, and reinterpret casting. I tried to find answers but somehow they all are confusing... Could anyone clears this concept. Thanks.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Check this great article[^]

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      G 1 Reply Last reply
      0
      • C Cedric Moonen

        Check this great article[^]

        Cédric Moonen Software developer
        Charting control [v3.0] OpenGL game tutorial in C++

        G Offline
        G Offline
        gothic_coder
        wrote on last edited by
        #3

        Cool, Thanks... I'm checking :)

        1 Reply Last reply
        0
        • G gothic_coder

          Hello All, I am always confused between static, dynamic, and reinterpret casting. I tried to find answers but somehow they all are confusing... Could anyone clears this concept. Thanks.

          S Offline
          S Offline
          Sauro Viti
          wrote on last edited by
          #4

          static_cast and reinterpret_cast work in the same way than the classic C-style cast, but they are used in two different context:

          • static_cast is used when casting between two related types, for instance from a numeric type to another. The conversion could generate a loss of data, but without the cast operator the code compile; the compiler could generate a warning.

            char a, b;
            int x = 123;
            double y = 123.456;
            a = static_cast<char>(x); // From int to char
            b = static_cast<char>(y); // From double to char

          • reinpterpret_cast is used when casting between two unrelated types, usually pointers, for instance from a pointer to a struct to a pointer to char. Without the cast operator the code doesn't compile; the compiler generate an error.

            POINT pt = { 10, 21 };
            unsigned char *pb = reinterpret_cast<unsigned char *>(&pt);
            // Print to screen the content of the structure in raw format
            for(int i = 0; i < sizeof(POINT); i++)
            printf("%02X ", pb[i]);

          Finally, dynamic_cast is a polymorphic cast operator; it's used only with pointers, and its main property is that if the conversion is possible it return the pointer value, otherwise it returns NULL. It's useful when working with inheritance.

          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