How can I create a generic pointer?
-
Hi list, I want to ask whether is it possible to have a pointer whose type can change according to the input. I want to make it clear waht I need by a little code below which is not possible to compile. I think there must be a very well known trick for this. Thanks in advance for your help. switch( datatype ) { case DT_INT8: char *myPntr = (char *)dataPntr case DT_UINT8: unsigned char *myPntr = (unsigned char*)dataPntr; case DT_INT16: short *myPntr = (short *)dataPntr; } dataPntr is a (void *). how can I create such myPntr? xonobo
-
Hi list, I want to ask whether is it possible to have a pointer whose type can change according to the input. I want to make it clear waht I need by a little code below which is not possible to compile. I think there must be a very well known trick for this. Thanks in advance for your help. switch( datatype ) { case DT_INT8: char *myPntr = (char *)dataPntr case DT_UINT8: unsigned char *myPntr = (unsigned char*)dataPntr; case DT_INT16: short *myPntr = (short *)dataPntr; } dataPntr is a (void *). how can I create such myPntr? xonobo
-
Hi list, I want to ask whether is it possible to have a pointer whose type can change according to the input. I want to make it clear waht I need by a little code below which is not possible to compile. I think there must be a very well known trick for this. Thanks in advance for your help. switch( datatype ) { case DT_INT8: char *myPntr = (char *)dataPntr case DT_UINT8: unsigned char *myPntr = (unsigned char*)dataPntr; case DT_INT16: short *myPntr = (short *)dataPntr; } dataPntr is a (void *). how can I create such myPntr? xonobo
xonobo wrote:
want to ask whether is it possible to have a pointer whose type can change according to the input.
A
void*
can point to anything, but in turn it looses the information to what it is pointing. You need to bundle this information with it. In VB this is done using the VARIANT struct. That is a struct holding a type marker and a piece of memory. Via a union, all datatypes are 'layered" above each other, using the same memory.
Failure is not an option - it's built right in.
-
xonobo wrote:
want to ask whether is it possible to have a pointer whose type can change according to the input.
A
void*
can point to anything, but in turn it looses the information to what it is pointing. You need to bundle this information with it. In VB this is done using the VARIANT struct. That is a struct holding a type marker and a piece of memory. Via a union, all datatypes are 'layered" above each other, using the same memory.
Failure is not an option - it's built right in.
-
You don't tell enough to get a solution to your problem. What are you trying to accomplish and why can't this be solved in another way? Does the (Microsoft specific) _variant_t help you?
Failure is not an option - it's built right in.
-
You don't tell enough to get a solution to your problem. What are you trying to accomplish and why can't this be solved in another way? Does the (Microsoft specific) _variant_t help you?
Failure is not an option - it's built right in.
-
Thamks for your help. I thought VARIANT is only available in VB that's why I asked more. Now I will check _variant_t. Thanks again.
xonobo wrote:
I thought VARIANT is only available in VB...
It's definitely not limited to VB.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-