Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

void * to class

Status
Not open for further replies.

gregbackus

Programmer
May 15, 2001
4
US
say you have a file like this...

#define CASTING_IS_A_BEAR 200

#include <string.h>
#include <iostream.h>

class thing { public:
char name[32];
thing() { strcpy(name,&quot;greg&quot;); }
};

int main(void)
{ void *p;
thing g;
p=(void*)g;
/*
now how do I get to g::name through p?
*/
cout<<(thing*)p.name<<endl; // does not work
return CASTING_IS_A_BEAR;
}

I have dumbed down the example to get to the root of the problem I am having.
I have a class (class A) that includes a pointer to another class (class B) and would
like class B to be able to keep track of the class A's that refer to them.
Any help would be greatly appreciated.
Greg
 
Just some minor errors, thats all:

int main(void)
{ void *p;
thing g;
p=(void*)&g;
/*
now how do I get to g::name through p?
*/
cout<<(thing*)p->name<<endl; // does not work
return CASTING_IS_A_BEAR;
}

That should take care of it. Just an ampersand and a pointer problem.

Matt

 
to save a step.. you don't actually have to do any typecasting with void pointers

eg:
p=&g; and cout << p->name;
 
I agree with 1/2 of that.

p=&g will work fine but the cout<<p->name will not. With the cout, a cast will be required because it is a void pointer.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top