from void* back to an object

Discussion in 'C' started by mad_girl, Sep 17, 2007.

  1. mad_girl

    mad_girl New Member

    Joined:
    Aug 14, 2007
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    now here's the problem
    i'm trying to make a linked list store different types of data object (which i don know what they r now)
    so i used void* like this
    //for example a node of int
    node<userDefindData>* n;
    void* ptr=n;
    and now i have the address of my node in my ptr ,but how can i treat this pointer as an object
    and call its functions???????
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,336
    Likes Received:
    377
    Trophy Points:
    83
    Make an object pointer and cast the void pointer to the object pointer and that should allow you to call the functions of that object.
     
  3. mad_girl

    mad_girl New Member

    Joined:
    Aug 14, 2007
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    firstly if i'm using node to store userDefinedData1, userDefinedData2, userDefinedData3 and i don know now which of them this pointer refering to so i can't make the cast..
    secondly it gives me an error on a statement like this
    node<userDefinedData>* k=dynamic_cast<node<userDefindData>*>(ptr);
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,336
    Likes Received:
    377
    Trophy Points:
    83
    If you do not know what type of object it is you should also not know what function to call.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice