I'm obviously very new to c++. I have a class Vector2<T> and I'm trying to cast a void pointer to it using a static_cast. However the ide(xcode) seems to not accept the syntax I am using.
I get this error:
Expected '>'
Here's the code:
deletestatic_cast<Vector2<float>*>(value); //value is of type void*
Also,
1. Why have you written your own vector class?
2. Why are you trying to cast to a Vector2<Float>* ?
3. Why are you trying to delete the result of the cast?
Okay, I'm using void pointers because I need an vector of attributes that can be of many types float, int, double, Vector2(custom class), Matrix ... etc. It seems to me that void pointers are the best way to do this. The reason I need to cast before deleting is that one simply cannot delete a void pointer. Please tell me if there are any glaring weakness in this design. Thanks!
You say you need a vector of attributes that can be of different types. But unless you know the type of it you cannot utilise it appropriately. At some point you have to know the type, so you shouldn't need to cast.
e.g
1 2 3 4 5 6 7
vector<void*> attributes;
attributes.push_back(/* float */);
attributes.push_back(/* string */);
// Now I need to know that attributes[0] is a float to use it
You haven't provided a problem that I would say warrants the use of an any container. The use of these is highly discouraged because of the complexity involved.
I don't actually have a vector void. I have a vector<container> where container is a class which contains a type variable and a void pointer. But even if I know the type, don't I still have to cast the pointer back to the original type to use the data.