Sign in to follow this  

Remember the type of a variable (C++)

This topic is 3736 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

Say I have a list of void* pointers. I know what type the values are when I create them. Later however I need to delete them. Deleting void pointers won't work so I need to cast to the correct type before calling delete. I knew what type they were when I created them, but not anymore when deleting them, now I've only got the list of void* pointers left. Is there some way in C++ to also keep a list that contains the types that these void* pointers are? That is, can you remember a type of something and use that information to cast something to the correct type again? I know there exists type_info, but I didn't find a way to use it for my purpose, it can return a string with the name but that's not enough to use it to cast your void pointer to the correct type...

Share this post


Link to post
Share on other sites
I'm not sure what kind of data your storing that those pointers link to, but you could have a interface class, say "class IDispose" which has its destructor made "virtual" so that the inheriting class can override it. Then all of your objects that are in that list would have to inherit from IDispose and have their dtor defined as "virtual".

Then just make the void* list a IDispose* list, then go through each element in the pointer list and say "delete element;".

Like say, I don't know what kind of data your storing (or even for what reasons), so I can't be sure if this will remedy all of of the type cases (ie, a pointer to a raw int* pointer).

Share this post


Link to post
Share on other sites
class Base {
public:
virtual ~Base() {}
};

class Concrete : public Base{
public:
virtual ~Concrete() {}

private:
int x;
};

template < class T >
class Typed : public Base {
public:
virtual ~Typed () {}
T value;
}

Base *x = new Concrete();
delete x;

Base *y = new Typed<double>();
delete y;





[Edited by - Antheus on September 19, 2007 10:03:28 AM]

Share this post


Link to post
Share on other sites
To expand on what ToohrVyk said....

....store a std::vector< std::pair< void*, void (void*) > >and

template<typename T>
void delete_ptr(void* ptr)
{
delete reinterpret_cast<T*>(ptr);
}

template<typename T>
void push_back_typed(std::vector< std::pair< void*, void (void*) > > vec, T* elem)
{
vec.push_back(std::make_pair(reinterpret_cast<void*>(elem), &delete_ptr<T>));
}

void pop_back_typed(std::vector< std::pair< void*, void (void*) > > vec)
{
std::pair< void*, void (void*) >& b = vec.back();
b.second(b.first);
vec.pop_back();
}

Share this post


Link to post
Share on other sites
Then don't use void pointers. A type safe approach would be to define a base class for all the object types that your void pointer can point to. Then create a list of pointers to your base object. In addition to allowing you to delete the values pointed to by the pointers, this also disallows you to make pointers to classes that do not inherit from your base class, which can save you if you accedently try to do that.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
Quote:
Original post by kornman00
Quote:
Original post by Antheus
*** Source Snippet Removed ***


Typed isn't a subclass of Base ;p


Ah boo hoo. You know what I mean.

I fixed the original source.


Ah, now it matches something that I also produced, after reading kornman00's post in which he spoke about the interface with virtual destructor :)

Except that the template class has a T* as member in my case, and deletes it in its destructor.

Share this post


Link to post
Share on other sites

This topic is 3736 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.

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