Sign in to follow this  
silverphyre673

void pointers

Recommended Posts

silverphyre673    454
Void pointers are interesting beasts. I am currently creating a "register" for different types of objects, which are all derived from a base class "Object". Each "Object" has a unique ID in the register, which is assigned by the register and stored in the Object. I was going to have each object be a part of an std::map<UNIQUE_ID, void *>, so indexed by ID number. This is the only way I can think of to store each type of object without resorting to having a different array for each object. Will it work? If not, what should I do instead? And how exactly do you allocate memory for the objects? Would it be like this: std::map<UNIQUE_ID, void *> register; and later... register[42] = new ObjectTypeTwo; or what? Thanks. I really tried with google, and I am unfortunately a poor programmer (actually, family has money, little to none of which gets spent on my programming habit). Thanks again.

Share this post


Link to post
Share on other sites
Programmer16    2321
I haven't used the std::map yet, but I believe that you are correct on how to allocate memory for it.

And, if they're all derived from Object, then couldn't you just use:

std::map<UNIQUE_ID, Object*> register;
// and later
register[42] = new ObjectTypeTwo;
// I don't know how to deallocate memory for a map, but this is my guess
Object* pTemp = register[42];
delete pTemp;


This is my best guess, so hopefully it is helpful (sorry if it isn't).

Share this post


Link to post
Share on other sites
intrest86    742
void * are a very ugly beast (at least to me). They are very mucha holdover from C, and there are beteer ways to do things now. In your case, since everything is derived from Object, why not store Object*?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this