Archived

This topic is now archived and is closed to further replies.

Lethian

Pointer question

Recommended Posts

Im having a bit of trouble trying to get the syntax correct on this. I have a class with a void pointer and a struct im trying to assign data to. class node(){ public: private: void* data; } struct bob{ int a; } when i create a node and try to assign the data tmp = new node; tmp->data=new bob; *(tmp->data).a = i keep getting an error that says left of '.a' must point to class/struct/union. What syntax shoudl i be using to assign my struct to the value? Edited by - lethian on January 31, 2001 3:08:08 PM

Share this post


Link to post
Share on other sites
well, you dereferencing it twice..
see if you type
a->b
then it means excatly the same as
*(a).b

*a returns the actual value of a(not a copy, the real thing), and now you can use the "." operator access a''s members..
-> is just a neat combo for you to use instead of typing *(a). over and over again

i just figured its a problem as well that you''re dereferencing a void pointer, try this:

((bob*)tmp.data)->a

you must cast your void* to a bob*

Share this post


Link to post
Share on other sites
    
class node{
public:
private:
void* data;

};

struct bob{
int a;
};

tmp = new node;
tmp->data=new bob;

bob* tmpBob = (bob*)tmp->data; // Cast to bob*

tmpBob->a = 5;


A couple of notes
  • No () after class node{
  • ; at the end of class/struct defines
  • "data" was private so you could not access it ( if outside class )





-------
Andrew

Edited by - acraig on January 31, 2001 4:09:20 PM

Share this post


Link to post
Share on other sites
Sorry about my last post. Try:

((bob*)(tmp->data))->a = 5;

You still can not access data unless you change it to a public variable.



Edited by - Antknei on January 31, 2001 4:31:21 PM

Share this post


Link to post
Share on other sites