Hello Ladies and Gentlemen!
I'm working on a proc. generated platform and I came across this problem:
I want to tore in an vector some pointers to object of different nature; I searched google a bit and found a nice answer: void pointers.
The other side of the problem is a bit too complex for me: I need another function to take that vector and read it back (i.e. read whatever it's stored inside).
Direct dereferencing, I cannot do.
Typecasting manually (like passing two vectors, one containing the big messy void pointer list and another one with the type of the pointers) -> is quite useless.
In so many words my question is:
is there a way to pass a big array/vector/set with whatever inside and to get another function to read it back without creating a total mess?
I hope my question is clear, if not please tell me what you need to know. Thank you in advance!
If you are using C++, maybe you are looking for polymorphism.
If you are using C, you aren't looking it the way it should be.
Please tell us what language you use, and put a little bit of code illustrating what you want. (don't forget to use the code tags).
I am guessing now but you use C++, don't you?
You need to cast the void ptr to the type you want:
vector<void *> data; (my_object *)data[5];
I am guessing now but you use C++, don't you?
You need to cast the void ptr to the type you want:
vector<void *> data;
(my_object *)data[5];
And one should ad, that this cast will miserably fail when there is an object with another type in the vector then expected.
And one should ad, that this cast will miserably fail when there is an object with another type in the vector then expected.
Indeed, but the OP's question indicates he is not ready for boost::any or polymorphism yet.
Thanks for the answers!
I'm using C++ (see that little icon on the left of the thread's title?).
@aximilar:
that's exactly my problem: I don't want to cast the void pointer to the type I want. (better, I don't like the solution
).
But let the code do the talk
current:
function: "hey, there's a big vector full of apples! yay!" function: "let's sort/do something else with them!" function: "hey, there's a big vectors of oranges" etc. etc.
It works, but it does not make me smile.
Something like:
function: "hello tiny pointer!" pointer: "hello big function!" function: "what kind of thing do you point to?" pointer: "an apple!" function: "yay, I like apples!" pointer: "yay!" function: "let's dereference you and do something with it!"
would do both
They should inherit something from a common parent. Otherwise, I question why they need to be stored in the same vector.
Yeap, do what Matthew says:
class Fruit { } class Apple : Fruit { } class Orange : Fruit { } //a vector for apples and oranges vector<Fruit *> fruits;
I searched google a bit and found a nice answer: void pointers.
void* is rarely a nice answer. You should use them really only when you absolutely have to. Use something like boost::any instead.
Mhhh, seems simple enough. I'll try it and report if I've found any problem. Thanks!