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. casting problem

casting problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++databasedata-structuresquestion
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.
  • J Offline
    J Offline
    Jerome Conus
    wrote on last edited by
    #1

    Hi ! In my app, I'm using a class that I didn't develop (and I have no access to the source code), which implements a linked list where pointers are stored with the type long. From this list, when I'm extracting a pointer at a specific index, I used to cast it into the needed type, for instance : Entity* pEntity=(Entity*)MyList->GetPointerAt(3); But now, I would like to get rid of these old C casts and use the C++ cast. Here is what I did : Entity* pEntity=static_cast(MyList->GetPointerAt(3)); But now, the compiler is rejecting this line, telling that the cast from 'long' to 'Entity*' is not possible. Anyone could tell me how I should implement this cast with only C++ casts ? Thank you for your help ! Jerome

    I J 2 Replies Last reply
    0
    • J Jerome Conus

      Hi ! In my app, I'm using a class that I didn't develop (and I have no access to the source code), which implements a linked list where pointers are stored with the type long. From this list, when I'm extracting a pointer at a specific index, I used to cast it into the needed type, for instance : Entity* pEntity=(Entity*)MyList->GetPointerAt(3); But now, I would like to get rid of these old C casts and use the C++ cast. Here is what I did : Entity* pEntity=static_cast(MyList->GetPointerAt(3)); But now, the compiler is rejecting this line, telling that the cast from 'long' to 'Entity*' is not possible. Anyone could tell me how I should implement this cast with only C++ casts ? Thank you for your help ! Jerome

      I Offline
      I Offline
      iceage
      wrote on last edited by
      #2

      see this: http://www.codeguru.com/forum/showthread.php?s=&threadid=253032

      1 Reply Last reply
      0
      • J Jerome Conus

        Hi ! In my app, I'm using a class that I didn't develop (and I have no access to the source code), which implements a linked list where pointers are stored with the type long. From this list, when I'm extracting a pointer at a specific index, I used to cast it into the needed type, for instance : Entity* pEntity=(Entity*)MyList->GetPointerAt(3); But now, I would like to get rid of these old C casts and use the C++ cast. Here is what I did : Entity* pEntity=static_cast(MyList->GetPointerAt(3)); But now, the compiler is rejecting this line, telling that the cast from 'long' to 'Entity*' is not possible. Anyone could tell me how I should implement this cast with only C++ casts ? Thank you for your help ! Jerome

        J Offline
        J Offline
        jhwurmbach
        wrote on last edited by
        #3

        You need to use

        Entity* pEntity = reinterpret_cast < Entitiy* > (MyList->GetPointerAt(3));

        because you are casting an number (your long-variable)into something completely different: A pointer to an Entity-Object. Static_cast can cast between differnt types of numbers, but you need to get something stronger to cast a number into a pointer. And that is reinterpret_cast.


        My opinions may have changed, but not the fact that I am right.

        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