• Advertisement
Sign in to follow this  

void pointers

This topic is 4646 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
Sign in to follow this  

  • Advertisement