casting problem
-
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
-
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
-
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
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 anEntity
-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 isreinterpret_cast
.
My opinions may have changed, but not the fact that I am right.