×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

void * to class

void * to class

void * to class

(OP)
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,"greg"); }
};

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

RE: void * to class

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

RE: void * to class

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

eg:
p=&g; and cout << p->name;

RE: void * to class

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close