Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

sprinter_trueno

run-time cast possible in c++ ?

This topic is 5531 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
of course it is. What exactly is your level of knowledge in C++? Casting is a basic staple of understanding it.

Share this post


Link to post
Share on other sites
u32 MyArray[10];
u32 *MyPointer=MyArray;
(u8*)MyPointer=MyArray;
(u16*)MyPointer=MyArray;

i _hope_ this works *G*

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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 else

if(...)
{
// 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 get

DWORD 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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  

char Array[4];
void* pArray = (void*)Array;
// Somewhere else

if(...)
{
// 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 ] ::::

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!