run-time cast possible in c++ ?

Started by
13 comments, last by sprinter_trueno 20 years, 10 months ago
Hi ppl, as stated in the topic I''d like to know whether it is possible to change a type at runtime. I have a pointer pointing to an array. The type of the pointer should vary (char, short, int) depending on certain circumstances so that I can read back a selective amount of data from the array. Is that possible somehow ? Lorenz
Advertisement
of course it is. What exactly is your level of knowledge in C++? Casting is a basic staple of understanding it.
daerid@gmail.com
Well, since you pointed out my inexperience, could you also provide me with an example ?

Would be very appreciated.
u32 MyArray[10];
u32 *MyPointer=MyArray;
(u8*)MyPointer=MyArray;
(u16*)MyPointer=MyArray;

i _hope_ this works *G*
------------------------------------------------------------Jawohl, Herr Oberst!
if you want a generic object type that can hold different kinds of objects, check this out
http://www.cuj.com/documents/s=8034/cuj0010cacciola/cacciola.htm

for general casting, check out
http://www.c-view.org/tech/pattern/cpptips/TypeCheck.html

sorry for ap:ing, i´m in a hurry

later
jens.
maximAL: nice try , thanks anyways.
AP: I'm on it, but didn't find anything yet.
daerid: I'm still waiting

Just to make things clear.


    char Array[4];void* pArray = (void*)Array;// Somewhere elseif(...){// Change the type of the pointer somehow to int*// ...}else if(...){// Change the type of the pointer somehow to short*// ...}else(...){// Change the type of the pointer somehow to char*// ...}// Read back whatever I getDWORD dwReadBack = pArray[0];  


If it is impossible, and that would be my guess, then how do this in a 'really neat' way. I already have solutions but they all suck grace.

Lorenz

[edited by - sprinter_trueno on May 29, 2003 2:58:06 PM]
If you have void *pvArray, you cannot change what type pvArray is, but you can change how you access it.

If you want to access it as another type just use
(char *)pvArray
(short *)pvArray
(long *)pvArray

Get it?


[edited by - BeerNutts on May 29, 2003 3:04:10 PM]

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

exactly what i wanted to say...
------------------------------------------------------------Jawohl, Herr Oberst!

     char Array[4];void* pArray = (void*)Array;// Somewhere elseif(...){    // NOTE: This will not just get you the first element    // of the array. It will get you ALL 4 elements.    int iReadBack = *(int*)pArray;}else if(...){    // This will get you 2 elements    short iReadBack = *(short*)pArray;}else(...){    // This will get you one.    char cReadBack = *(char*)pArray;}  


The reason for the different size retrievals is that the array was declasred as a 4 element char array. Each char is 1 byte, so the array size is 4 bytes.

an int is 4 bytes, so by casting to an int you will be retirieving a value 4 bytes long, therefor extracting all 4 bytes of the array when derefrencing the pointer. To better understand it you have to take into account what beernuts said: "you cannot change what type pvArray is, but you can change how you access it"

say for example I have a variable

DWORD var = 0xFFFFFFFF;

that variable is chock-a-block. The value cannot have a higher number, if you add one it will roll back to 0.

If you just want to extract 1 byte from that, you can get a char pointer and point it to the variable, then when you dereference teh char pointer it will get you 1 bytes worth of data. a short will get you 2 bytes, etc...

char* pCharVar = (char*)&var

int iOneByteValue = *pCharVar;

If I''m not mistaken, iOneByteValue will have the value 0xFF inside it. One full byte, (255).

:::: [ Triple Buffer V2.0 ] ::::
[size=2]aliak.net
quote:Original post by sprinter_trueno
Well, since you pointed out my inexperience, could you also provide me with an example ?

Would be very appreciated.


I apologize, I was having a bad morning.
daerid@gmail.com

This topic is closed to new replies.

Advertisement