|
|
| Type casting and void pointer (or, how to pass anthing in a vector) |
|
bicilotti
Member #9,995
July 2008
|
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). 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! ----- |
|
GullRaDriel
Member #3,861
September 2003
|
If you are using C++, maybe you are looking for polymorphism. 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). "Code is like shit - it only smells if it is not yours" |
|
axilmar
Member #1,204
April 2001
|
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];
|
|
count
Member #5,401
January 2005
|
Quote: 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; And one should ad, that this cast will miserably fail when there is an object with another type in the vector then expected.
|
|
axilmar
Member #1,204
April 2001
|
Quote: 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. |
|
bicilotti
Member #9,995
July 2008
|
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 ----- |
|
Matthew Leverton
Supreme Loser
January 1999
|
They should inherit something from a common parent. Otherwise, I question why they need to be stored in the same vector. |
|
axilmar
Member #1,204
April 2001
|
Yeap, do what Matthew says: class Fruit { } class Apple : Fruit { } class Orange : Fruit { } //a vector for apples and oranges vector<Fruit *> fruits;
|
|
Speedo
Member #9,783
May 2008
|
Quote: 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. |
|
bicilotti
Member #9,995
July 2008
|
Mhhh, seems simple enough. I'll try it and report if I've found any problem. Thanks! ----- |
|
|