CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2006
    Location
    Croatia - Zagreb
    Posts
    459

    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?

  2. #2
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    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?

  3. #3
    Join Date
    Feb 2006
    Location
    Croatia - Zagreb
    Posts
    459

    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?

  4. #4
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    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

  5. #5
    Join Date
    Feb 2006
    Location
    Croatia - Zagreb
    Posts
    459

    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?

  6. #6
    Join Date
    Jun 2006
    Location
    Bangalore, India
    Posts
    332

    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.

  7. #7
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: How to determine the type of variable?

    Quote 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....
         };
    };

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured