<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Type casting and void pointer (or, how to pass anthing in a vector)</title>
		<link>http://www.allegro.cc/forums/view/599518</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 11 Mar 2009 00:47:07 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hello Ladies and Gentlemen!</p><p>I&#39;m working on a proc. generated platform and I came across this problem:</p><p>I want to tore in an vector some pointers to object of different nature; I searched google a bit and found a nice answer: <i>void pointers</i>.</p><p>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&#39;s stored inside).<br />Direct dereferencing, I cannot do.<br />Typecasting manually (like passing two vectors, one containing the big messy void pointer list and another one with the type of the pointers) -&gt; is quite useless.</p><p>In so many words my question is:</p><p>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?</p><p>I hope my question is clear, if not please tell me what you need to know. Thank you in advance!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bicilotti)</author>
		<pubDate>Tue, 10 Mar 2009 20:13:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If you are using C++, maybe you are looking for polymorphism. <br />If you are using C, you aren&#39;t looking it the way it should be.</p><p>Please tell us what language you use, and put a little bit of code illustrating what you want. (don&#39;t forget to use the <a href="http://www.allegro.cc/mockup.html">code tags</a>).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (GullRaDriel)</author>
		<pubDate>Tue, 10 Mar 2009 20:19:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I am guessing now but you use C++, don&#39;t you?</p><p>You need to cast the void ptr to the type you want:</p><div class="source-code snippet"><div class="inner"><pre>vector<span class="k3">&lt;</span><span class="k1">void</span> <span class="k3">*</span><span class="k3">&gt;</span> data<span class="k2">;</span>
<span class="k2">(</span>my_object <span class="k3">*</span><span class="k2">)</span>data<span class="k2">[</span><span class="n">5</span><span class="k2">]</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Tue, 10 Mar 2009 20:34:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I am guessing now but you use C++, don&#39;t you?</p><p>You need to cast the void ptr to the type you want:</p><p>vector&lt;void *&gt; data;<br />(my_object *)data[5];
</p></div></div><p>

And one should ad, that this cast will miserably fail when there is an object with another type in the vector then expected.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (count)</author>
		<pubDate>Tue, 10 Mar 2009 20:44:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
And one should ad, that this cast will miserably fail when there is an object with another type in the vector then expected.
</p></div></div><p>

Indeed, but the OP&#39;s question indicates he is not ready for boost::any or polymorphism yet.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Tue, 10 Mar 2009 20:59:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for the answers!</p><p>I&#39;m using C++ (see that little icon on the left of the thread&#39;s title?).</p><p>@aximilar:</p><p>that&#39;s <i>exactly</i> my problem: I don&#39;t want to cast the void pointer to the type I want. (better, I don&#39;t like the solution <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />).</p><p>But let the code do the talk</p><p>current:
</p><div class="source-code snippet"><div class="inner"><pre>function: <span class="s">"hey, there's a big vector full of apples! yay!"</span>
function: <span class="s">"let's sort/do something else with them!"</span>

function: <span class="s">"hey, there's a big vectors of oranges"</span> etc. etc.
</pre></div></div><p>

It works, but it does not make me smile.</p><p>Something like:</p><div class="source-code snippet"><div class="inner"><pre>function: <span class="s">"hello tiny pointer!"</span>
pointer: <span class="s">"hello big function!"</span>
function: <span class="s">"what kind of thing do you point to?"</span>
pointer: <span class="s">"an apple!"</span>
function: <span class="s">"yay, I like apples!"</span>
pointer: <span class="s">"yay!"</span>
function: <span class="s">"let's dereference you and do something with it!"</span>
</pre></div></div><p>

would do both
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bicilotti)</author>
		<pubDate>Tue, 10 Mar 2009 23:00:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>They should inherit something from a common parent. Otherwise, I question why they need to be stored in the same vector.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Leverton)</author>
		<pubDate>Tue, 10 Mar 2009 23:17:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeap, do what Matthew says:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">class</span> Fruit <span class="k2">{</span>
<span class="k2">}</span>

<span class="k1">class</span> Apple <span class="k2">:</span> Fruit <span class="k2">{</span>
<span class="k2">}</span>

<span class="k1">class</span> Orange <span class="k2">:</span> Fruit <span class="k2">{</span>
<span class="k2">}</span>

<span class="c">//a vector for apples and oranges</span>
vector<span class="k3">&lt;</span>Fruit <span class="k3">*</span><span class="k3">&gt;</span> fruits<span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Wed, 11 Mar 2009 00:37:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I searched google a bit and found a nice answer: void pointers.
</p></div></div><p>

void* is rarely a nice answer.  You should use them really only when you absolutely have to.  Use something like boost::any instead.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Speedo)</author>
		<pubDate>Wed, 11 Mar 2009 00:37:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Mhhh, seems simple enough. I&#39;ll try it and report if I&#39;ve found any problem. Thanks!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bicilotti)</author>
		<pubDate>Wed, 11 Mar 2009 00:47:07 +0000</pubDate>
	</item>
</rss>
