Hi All.
I'm passing void *ptr as argument in function and I have to find out is the argument pass as pointer to void, int, float or char*, anyone know this?
I was thinking about variant type. But I'm not quite sure.
Printable View
Hi All.
I'm passing void *ptr as argument in function and I have to find out is the argument pass as pointer to void, int, float or char*, anyone know this?
I was thinking about variant type. But I'm not quite sure.
You can't, you need to know in advance ... where do you find this need? Can you show the function? Can you use templates?
That is taken from my current project involving snmp.
my class looks like this
MibLeaf is base class that holds one of tree different values but it's values are wrepped in SnmpSyntax witch determines actuall value that is setted through MibLeaf::set_value(SnmpSyntax &); hence the dillema. I could use it's SnmpSyntax but the goal is to hide most of SNMP api from end programmer.Code:class I_cSnmpObject : public MibLeaf()
{
public:
I_cSnmpObject(string oid, void *ptr);
...
};
can I pass variant instead of void* and please consider that this code is to be ported to linut so potrability matters.
Never mind. Don't bother, I'll use SnmpSyntax...to give the boys something to think about :D
If string as a parameter then what is stopping u from having int/float/char.... as a parameter.Quote:
I_cSnmpObject(string oid, void *ptr);
string oid is objectID of that instance. but the value that instance contains can be one of three: char *, float , and int....hey wait a minute. How stupid do I get?? If numberof types is known what stops me from building constructor for each of the types. Man I'm going to throw myself through window.
But siriously, how come there nothing that can determine to witch type void pointer points?
void pointer is a generic datatype that can hold pointer to any data type; int, float, struct... . This means you cannot deference it unless you know what it points to by type casting it.
Well, of course you can use overloading. And if it helps you could use templates to provide a wider support:Quote:
Originally Posted by Odiee
It is not possible to get the type a void pointer points to because you actually lost that information the time you converted the specific pointer type to a void pointer. Additionally, you can have a structure for your class that has an "int type;" field as the "first" member and then have the three other types inside a union (part of the struct) and use it here instead of a void*. Something like this:Code:template<typename T>
I_cSnmpObject(string oid, T *ptr);
Code:struct typeWrapper
{
//type specifies what type value is "set" for the union that you can legally "get"
int type; //0 is int, 1 if char, and so on...
union somename
{
int i;
char j;
//.. and so on....
};
};