-
August 29th, 2006, 03:55 AM
#1
How to determine the type of variable if variable is void*
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.
Last edited by Odiee; August 29th, 2006 at 04:16 AM.
You just divided by zero, didn't you?
-
August 29th, 2006, 04:19 AM
#2
Re: How to determine the type of variable if variable is void*
You can't, you need to know in advance ... where do you find this need? Can you show the function? Can you use templates?
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
August 29th, 2006, 04:37 AM
#3
Re: How to determine the type of variable?
That is taken from my current project involving snmp.
my class looks like this
Code:
class I_cSnmpObject : public MibLeaf()
{
public:
I_cSnmpObject(string oid, void *ptr);
...
};
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.
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
You just divided by zero, didn't you?
-
August 29th, 2006, 04:59 AM
#4
Re: How to determine the type of variable?
I_cSnmpObject(string oid, void *ptr);
If string as a parameter then what is stopping u from having int/float/char.... as a parameter.
C++ program ran... C++ program crashed... C++ programmer quit !!
Regards
Shaq
-
August 29th, 2006, 05:34 AM
#5
Re: How to determine the type of variable?
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?
You just divided by zero, didn't you?
-
August 29th, 2006, 05:42 AM
#6
Re: How to determine the type of variable?
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.
-
August 29th, 2006, 06:45 AM
#7
Re: How to determine the type of variable?
Originally Posted by Odiee
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.
Well, of course you can use overloading. And if it helps you could use templates to provide a wider support:
Code:
template<typename T>
I_cSnmpObject(string oid, T *ptr);
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:
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....
};
};
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|