Remember the type of a variable (C++)

Started by
9 comments, last by Lode 16 years, 7 months ago
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...
Advertisement
Sounds to me like you have design issues - why do you need a list of void pointers? What are you doing with them?
No, you can't. You can store a deletion functor, though.
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).
boost::shared_ptr?

boost::shared_ptr<WhateverTypeYouLike> p(new WhateverTypeYouLike(x, y, z));boost::shared_ptr<void> q = p;


or boost::any?
boost::any a(WhateverTypeYouLike(x, y, z));
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]
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();}
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.
Quote:Original post by Antheus
*** Source Snippet Removed ***


Typed isn't a subclass of Base ;p

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.

This topic is closed to new replies.

Advertisement