Hello guys,
how can I figure out the types of elements in an array of void pointers ?
Because I guess I have to cast them, and in order to cast them I need their types.
Any ideas ?
You can't. All type information is lost when converting to a void pointer. The only way is to record the type information somewhere else... like as an enum or string or something in another array.
This is exactly why you should avoid using void pointers.
EDIT: actually I never tried using typeinfo on void pointer.... I kind of doubt it'd work but it's worth a try.
Assuming you mean delete as in the keyword delete (calling the object's dtor)...
It can't be done without casting to a known type, and there's no way to know what type you have with just a void pointer alone. You'd need to store type information elsewhere, like in a separate array.
EDIT:
This is a design problem. You should not be using void pointers. You need to design a more intelligent class hierarchy to retain type information.
The quick and dirty way is to use a "Object" base class. That is, instead of having an array of void pointers, create an abstract base class with a virtual destructor:
1 2 3 4 5
class Object
{
public:
virtual ~Object() { }
};
Derive all classes that can possibly be put in your array from this Object class. Then instead of making a void pointer array, make an Object pointer array:
1 2 3 4 5 6 7
//void* myarray[100]; // bad
Object* myarray[100]; // good
// then you can do this:
delete myarray[5]; // no casting necessary
I see.
But actually this is a task from my university...
int deleteItems(void *array, int itemSize, int itemsCount);
I have this function and I need to access some of the elements in that array, also to delete some of them, to reassign some also...
And that's why I'm looking for a way to do it with void pointers.
Regards
p.s.: Is it true that I can't dereference an element of an array like this :
1 2
void *array_ptr = my_array;
array_ptr[0];
but I can do it like this :
[code]
((void**)array_ptr)[1];
[code]
or something like that...
int deleteItems(void *array, int itemSize, int itemsCount);
Oh well hold on now, it's asking for itemSize, so are you keeping track of the size of the elements somewhere, or are you able to pass it directly to that function? Because you can pretty much figure out what type the element is from its size in memory.
Well either your professor is giving you absurd problems, or you're approaching the problem inappropriately. I can't say which without speculating as to what the problem actually is.
p.s.: Is it true that I can't dereference an element of an array like this :
Yes. You cannot dereference a void pointer unless you cast it to a known type.
but I can do it like this :
NO! That casts a void* to a void**, and then dereferences to return a void*. IE: it doesn't do what you expect at all.
These are all classic void* disaster situations. This is like the posterboard for why you should avoid C-style casts and void pointers.
Well, I guess I can find it's type by the intSize variable, but "int" and "float" are both 4 bytes. How do I know if it's pointing to float or to int ?
Look, sorry if my questions are too stupid, I just can't get used to these lower level things...
but "int" and "float" are both 4 bytes. How do I know if it's pointing to float or to int ?
Ya that's why I said you can only "pretty much" find out. The other thing is that you don't know whether or not it was signed or unsigned, though that won't really matter if all you are doing is deleting the element.
All I can think of right now is put the data in a string, and then check for a decimal point. If there is one, cast it to float, if not, cast it to an int. Though I don't really have experience using an array of void pointers with all different types, so I'm not sure if this is the best practice...
Disch is correct -- you have to post the context of the assignment. Chances are your approach is not right because any "solution" involving casting -- be it attempts at being smart such as Tevsky's last reply or stupid (blind) -- is only a fragile hack at best.