<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to make function work with arrays of different pointers?]]></title><description><![CDATA[<p>I hope you can understand english.</p>
<p>I have class _A and other classes derived from _A. I need to make a function that can work with an array of pointers to _A. And this function should work with an array of pointers to derived classes as well.</p>
<p>I write:</p>
<pre><code>class _A{...};
class _B: public _A{...};

void ShowArray(_A **Array, int size)
{
	for(int n(0); n&lt;size; ++n) cout &lt;&lt; Array[n]-&gt;memberA &lt;&lt; &quot; &quot;;
}
</code></pre>
<p>Then I can call this function like this:</p>
<pre><code>_A **ArrayA;
_B **ArrayB;

...

ShowArray(ArrayA,10);
ShowArray((_A**)ArrayB,10);
</code></pre>
<p>Everything is OK.</p>
<p>But what can I do if I need to work with an array &quot;vector&quot; from STL ?<br />
How can I write such a function that can work with vector&lt;_A *&gt; and every other array of pointers of derived of _A type?</p>
<p>Here is full code to understand a problem:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;

using namespace std;

class _A
{ public:
	int memberA;
	_A(int memberA):memberA(memberA){;}
};

class _B: public _A
{ public:
	int memberB;
	_B(int memberA,int memberB):_A(memberA),memberB(memberB){;}
};

void ShowArrayAOldStyle(_A **Array, int size)
{
	for(int n(0); n&lt;size; ++n)
		cout&lt;&lt;Array[n]-&gt;memberA&lt;&lt;&quot; &quot;;
	cout&lt;&lt;endl;
}

void ShowArrayANewStyle(vector&lt;_A *&gt; &amp;Array)
{
	for(vector&lt;_A *&gt;::size_type n(0); n&lt;Array.size(); ++n)
		cout&lt;&lt;Array[n]-&gt;memberA&lt;&lt;&quot; &quot;;
	cout&lt;&lt;endl;
}

void ShowElemA(_A *Elem)
{
	cout&lt;&lt;Elem-&gt;memberA&lt;&lt;endl;
}

int main(int argc, char **argv)
{
	int const numObjects = 3;

	_A **ArrayAOldStyle = new _A*[numObjects];
	_B **ArrayBOldStyle = new _B*[numObjects];
	vector&lt;_A *&gt; ArrayANewStyle(numObjects);
	vector&lt;_B *&gt; ArrayBNewStyle(numObjects);

	for(int n(0); n&lt;numObjects; ++n)
	{
		ArrayAOldStyle[n]=new _A(1);
		ArrayBOldStyle[n]=new _B(2,3);
		ArrayANewStyle[n]=new _A(4);
		ArrayBNewStyle[n]=new _B(5,6);
	}

	ShowElemA(ArrayAOldStyle[0]); //Good
	ShowElemA(ArrayBOldStyle[0]); //Good
	ShowElemA(ArrayANewStyle[0]); //Good
	ShowElemA(ArrayBNewStyle[0]); //Good

	ShowArrayAOldStyle(       ArrayAOldStyle,numObjects); //Good
	ShowArrayAOldStyle((_A**) ArrayBOldStyle,numObjects); //Good, but there is no automatic type conversion
	ShowArrayANewStyle(ArrayANewStyle); //Good
	ShowArrayANewStyle(ArrayBNewStyle); //BAD. What to do???

	for(int n(0); n&lt;numObjects; ++n)
	{
		delete ArrayAOldStyle[n];
		delete ArrayBOldStyle[n];
		delete ArrayANewStyle[n];
		delete ArrayBNewStyle[n];
	}

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/181330/how-to-make-function-work-with-arrays-of-different-pointers</link><generator>RSS for Node</generator><lastBuildDate>Tue, 22 Sep 2026 07:12:56 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/181330.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 12 May 2007 15:02:37 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to make function work with arrays of different pointers? on Sat, 12 May 2007 15:02:37 GMT]]></title><description><![CDATA[<p>I hope you can understand english.</p>
<p>I have class _A and other classes derived from _A. I need to make a function that can work with an array of pointers to _A. And this function should work with an array of pointers to derived classes as well.</p>
<p>I write:</p>
<pre><code>class _A{...};
class _B: public _A{...};

void ShowArray(_A **Array, int size)
{
	for(int n(0); n&lt;size; ++n) cout &lt;&lt; Array[n]-&gt;memberA &lt;&lt; &quot; &quot;;
}
</code></pre>
<p>Then I can call this function like this:</p>
<pre><code>_A **ArrayA;
_B **ArrayB;

...

ShowArray(ArrayA,10);
ShowArray((_A**)ArrayB,10);
</code></pre>
<p>Everything is OK.</p>
<p>But what can I do if I need to work with an array &quot;vector&quot; from STL ?<br />
How can I write such a function that can work with vector&lt;_A *&gt; and every other array of pointers of derived of _A type?</p>
<p>Here is full code to understand a problem:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;

using namespace std;

class _A
{ public:
	int memberA;
	_A(int memberA):memberA(memberA){;}
};

class _B: public _A
{ public:
	int memberB;
	_B(int memberA,int memberB):_A(memberA),memberB(memberB){;}
};

void ShowArrayAOldStyle(_A **Array, int size)
{
	for(int n(0); n&lt;size; ++n)
		cout&lt;&lt;Array[n]-&gt;memberA&lt;&lt;&quot; &quot;;
	cout&lt;&lt;endl;
}

void ShowArrayANewStyle(vector&lt;_A *&gt; &amp;Array)
{
	for(vector&lt;_A *&gt;::size_type n(0); n&lt;Array.size(); ++n)
		cout&lt;&lt;Array[n]-&gt;memberA&lt;&lt;&quot; &quot;;
	cout&lt;&lt;endl;
}

void ShowElemA(_A *Elem)
{
	cout&lt;&lt;Elem-&gt;memberA&lt;&lt;endl;
}

int main(int argc, char **argv)
{
	int const numObjects = 3;

	_A **ArrayAOldStyle = new _A*[numObjects];
	_B **ArrayBOldStyle = new _B*[numObjects];
	vector&lt;_A *&gt; ArrayANewStyle(numObjects);
	vector&lt;_B *&gt; ArrayBNewStyle(numObjects);

	for(int n(0); n&lt;numObjects; ++n)
	{
		ArrayAOldStyle[n]=new _A(1);
		ArrayBOldStyle[n]=new _B(2,3);
		ArrayANewStyle[n]=new _A(4);
		ArrayBNewStyle[n]=new _B(5,6);
	}

	ShowElemA(ArrayAOldStyle[0]); //Good
	ShowElemA(ArrayBOldStyle[0]); //Good
	ShowElemA(ArrayANewStyle[0]); //Good
	ShowElemA(ArrayBNewStyle[0]); //Good

	ShowArrayAOldStyle(       ArrayAOldStyle,numObjects); //Good
	ShowArrayAOldStyle((_A**) ArrayBOldStyle,numObjects); //Good, but there is no automatic type conversion
	ShowArrayANewStyle(ArrayANewStyle); //Good
	ShowArrayANewStyle(ArrayBNewStyle); //BAD. What to do???

	for(int n(0); n&lt;numObjects; ++n)
	{
		delete ArrayAOldStyle[n];
		delete ArrayBOldStyle[n];
		delete ArrayANewStyle[n];
		delete ArrayBNewStyle[n];
	}

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1283522</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283522</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Sat, 12 May 2007 15:02:37 GMT</pubDate></item><item><title><![CDATA[Reply to How to make function work with arrays of different pointers? on Sat, 12 May 2007 15:14:35 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p>I hope you can understand english.</p>
</blockquote>
<p>Und ich hoffe du kannst Deutsch.</p>
<p>SAn schrieb:</p>
<blockquote>
<p>I have class _A and other classes derived from _A.</p>
</blockquote>
<p>Nein. Namen, die mit _ anfangen gehören dem Compiler, so darfst du deine Klasse nicht nennen.</p>
<pre><code class="language-cpp">template &lt;typename T&gt;
void ShowArrayAEvenNewerStyle(std::vector&lt;T*&gt; const&amp; Array)
{
    for(std::vector&lt;T*&gt;::const_iterator i = Array.begin (); i != Array.end (); ++i)
        std::cout &lt;&lt; i-&gt;memberA &lt;&lt; ' ';
    std::cout &lt;&lt; std::endl;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1283535</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283535</guid><dc:creator><![CDATA[.filmor]]></dc:creator><pubDate>Sat, 12 May 2007 15:14:35 GMT</pubDate></item><item><title><![CDATA[Reply to How to make function work with arrays of different pointers? on Sat, 12 May 2007 15:19:43 GMT]]></title><description><![CDATA[<p>don't know if there is a better way but u can try this.</p>
<pre><code class="language-cpp">template&lt;typename T&gt; 
void ShowArrayANewStyle(T &amp;Array)
{
    for(vector&lt;_A *&gt;::size_type n(0); n&lt;Array.size(); ++n)
        cout&lt;&lt;Array[n]-&gt;memberA&lt;&lt;&quot; &quot;;
    cout&lt;&lt;endl;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1283542</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283542</guid><dc:creator><![CDATA[fgdfsdg]]></dc:creator><pubDate>Sat, 12 May 2007 15:19:43 GMT</pubDate></item><item><title><![CDATA[Reply to How to make function work with arrays of different pointers? on Sat, 12 May 2007 15:26:02 GMT]]></title><description><![CDATA[<p>Thank you.</p>
<p>I am using &quot;_&quot; to distinguish between types (classes) names and objects names.</p>
<p>The solution you provided is works. But there is no automatic checking of the inheritance. A user of such function can provide array of every type to the function.</p>
<p>Is it possible to say compiler that class T in your example MUST be derived from _A class?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1283546</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283546</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Sat, 12 May 2007 15:26:02 GMT</pubDate></item><item><title><![CDATA[Reply to How to make function work with arrays of different pointers? on Sat, 12 May 2007 15:32:53 GMT]]></title><description><![CDATA[<p>.filmor schrieb:</p>
<blockquote>
<p>SAn schrieb:</p>
<blockquote>
<p>I hope you can understand english.</p>
</blockquote>
<p>Und ich hoffe du kannst Deutsch.</p>
</blockquote>
<p>Idiot.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1283552</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283552</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Sat, 12 May 2007 15:32:53 GMT</pubDate></item><item><title><![CDATA[Reply to How to make function work with arrays of different pointers? on Sat, 12 May 2007 15:33:00 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p>I am using &quot;_&quot; to distinguish between types (classes) names and objects names.</p>
</blockquote>
<p><a href="http://www.cse.iitb.ac.in/~karkare/Gc/coding/" rel="nofollow">http://www.cse.iitb.ac.in/~karkare/Gc/coding/</a></p>
<blockquote>
<p>Names starting with _ (underscore) and __ (two underscores) are reserved for compiler writers. These should be avoided.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/1283554</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283554</guid><dc:creator><![CDATA[1234567654]]></dc:creator><pubDate>Sat, 12 May 2007 15:33:00 GMT</pubDate></item><item><title><![CDATA[Reply to How to make function work with arrays of different pointers? on Sat, 12 May 2007 15:40:57 GMT]]></title><description><![CDATA[<p>OK. I will try to avoid names starting with &quot;_&quot;</p>
<p>And I did not write the last message containing &quot;idiot&quot;. This is a someone's joke.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1283561</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283561</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Sat, 12 May 2007 15:40:57 GMT</pubDate></item><item><title><![CDATA[Reply to How to make function work with arrays of different pointers? on Sat, 12 May 2007 16:23:06 GMT]]></title><description><![CDATA[<p>just pass to the function by pointer/ref to base.</p>
<pre><code class="language-cpp">class A
{
};

class B : public A
{
};

class C
{
};

void func(A const&amp; val)
{ //do something
}

int main(int, char**)
{
 A a;
 B b;
 C c;

 func(a); //works
 func(b); //should work as well
 func(c); //compile time error
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1283594</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283594</guid><dc:creator><![CDATA[inp]]></dc:creator><pubDate>Sat, 12 May 2007 16:23:06 GMT</pubDate></item><item><title><![CDATA[Reply to How to make function work with arrays of different pointers? on Sat, 12 May 2007 16:29:24 GMT]]></title><description><![CDATA[<p><strong>inp</strong>, yes. The problem is to do the same behaviour with <em>arrays</em> of pointers, not just with pointers.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1283595</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283595</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Sat, 12 May 2007 16:29:24 GMT</pubDate></item><item><title><![CDATA[Reply to How to make function work with arrays of different pointers? on Sat, 12 May 2007 16:44:56 GMT]]></title><description><![CDATA[<p>how about using functors and foreach/transform ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1283604</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283604</guid><dc:creator><![CDATA[ronny]]></dc:creator><pubDate>Sat, 12 May 2007 16:44:56 GMT</pubDate></item><item><title><![CDATA[Reply to How to make function work with arrays of different pointers? on Sat, 12 May 2007 16:51:42 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p><strong>inp</strong>, yes. The problem is to do the same behaviour with <em>arrays</em> of pointers, not just with pointers.</p>
</blockquote>
<p>yes, i got that.</p>
<p>but A** isnt B**.<br />
anyway A** can store elements of type B*<br />
so why not use a constistent scheme and use just basettype pointerarrays</p>
<pre><code class="language-cpp">class A
{
public:
	virtual void WhoAmI()
	{	printf(&quot;A\n&quot;);
	}
};

class B: public A
{
public:
	virtual void WhoAmI()
	{	printf(&quot;B\n&quot;);
	}
};

void func(A** arr, size_t num)
{
	for (size_t i = 0; i &lt; num; ++i)
		arr[i]-&gt;WhoAmI();
}

int main(argc, char**)
{
	A** a = new A*[1];
	A** b = new A*[1];

	a[0] = new A();
	b[0] = new B();

	func(a, 1);
	func(b, 1);

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1283611</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283611</guid><dc:creator><![CDATA[inp]]></dc:creator><pubDate>Sat, 12 May 2007 16:51:42 GMT</pubDate></item><item><title><![CDATA[Reply to How to make function work with arrays of different pointers? on Sat, 12 May 2007 17:02:03 GMT]]></title><description><![CDATA[<p>sorry for my sloppy coding though <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":-)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1283619</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283619</guid><dc:creator><![CDATA[inp]]></dc:creator><pubDate>Sat, 12 May 2007 17:02:03 GMT</pubDate></item><item><title><![CDATA[Reply to How to make function work with arrays of different pointers? on Sat, 12 May 2007 17:14:53 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p><strong>inp</strong>, yes. The problem is to do the same behaviour with <em>arrays</em> of pointers, not just with pointers.</p>
</blockquote>
<p>That is not possible (well, unless you write your own array/vector-class to simulate it - and then there's more than one way to do it too). The reason is simply that when you convert a pointer to a derived class to a pointer to an (accessible and unambigous) base class that converted pointer ceases to point to your derived class. What you can do is converting rvalue pointers, i.e. the values of pointer objects. What you cannot do is to convert lvalues of pointer type to other lvalues of pointer type which pointed to different objects. And that is really what you do when you have an array</p>
<pre><code class="language-cpp">struct Foo {};
struct Bar : Foo {};
Bar* x[N];
</code></pre>
<p>and try to cast it</p>
<pre><code class="language-cpp">(Foo*(&amp;)[N])x;
</code></pre>
<p>Because that means that you want to treat each element of x as if it was an object containing a pointer to Foo. For one, pointers to different types may not have the same object representation. But more importantly, they may not even have the same value when converting one to another (using a suitable point of reference: void*). One pointer points to the Bar object and the other to the Foo subobject within the Bar object and those two need not share the same address. As a general rule - laid down in ancient C times - two different (living - so we ignore unions here) objects do not have the same address. There are only two exceptions to that: the first member of class may have the same address as the class itself (and in case of PODs that is required) and a base class subobject may (but need not) have the same address as the object of the derived class.</p>
<p>This comes down to a simple rule: never treat arrays polymorphically. Polymorphism applies to <em>individual</em> pointers and references only, never to collections of them.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1283629</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283629</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sat, 12 May 2007 17:14:53 GMT</pubDate></item><item><title><![CDATA[Reply to How to make function work with arrays of different pointers? on Sat, 12 May 2007 17:50:00 GMT]]></title><description><![CDATA[<p>I can not use virtual functions due to performance issues.</p>
<p>But the previous messages bring me to the following idea:</p>
<pre><code class="language-cpp">inline void CheckType(A *a){;}

template &lt;typename T&gt; void ShowArrayA(vector&lt;T*&gt; &amp;Array)
{
  if(!Array.size()) return;

  CheckType(Array[0]); //Check if T derived from A. I think the clever compiler will generate empty code for this

  for(vector&lt;T*&gt;::size_type n(0); n&lt;Array.size(); ++n)
    cout &lt;&lt; Array[n]-&gt;memberA &lt;&lt; ' ';

  cout &lt;&lt; endl;
}
</code></pre>
<p>This looks ugly, but I think it should work.</p>
<p><strong>camper</strong>, thank you for the detailed explanation.</p>
<blockquote>
<p>But more importantly, they may not even have the same value when converting one to another</p>
</blockquote>
<p>This is new to me. I thought that each pointer points to the very first byte of the object. And base class subobject always have the same address as the object of the derived class.</p>
<p>But now I understand that it is compiler-specific.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1283639</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283639</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Sat, 12 May 2007 17:50:00 GMT</pubDate></item><item><title><![CDATA[Reply to How to make function work with arrays of different pointers? on Sat, 12 May 2007 17:52:51 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p>I can not use virtual functions due to performance issues.</p>
</blockquote>
<p>there are plenty ways how to counter performance problems.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1283640</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283640</guid><dc:creator><![CDATA[inp]]></dc:creator><pubDate>Sat, 12 May 2007 17:52:51 GMT</pubDate></item><item><title><![CDATA[Reply to How to make function work with arrays of different pointers? on Sat, 12 May 2007 17:59:56 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<blockquote>
<p>But more importantly, they may not even have the same value when converting one to another</p>
</blockquote>
<p>This is new to me. I thought that each pointer points to the very first byte of the object. And base class subobject always have the same address as the object of the derived class.</p>
<p>But now I understand that it is compiler-specific.</p>
</blockquote>
<p>Think multiple inheritance.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1283645</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283645</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sat, 12 May 2007 17:59:56 GMT</pubDate></item><item><title><![CDATA[Reply to How to make function work with arrays of different pointers? on Sat, 12 May 2007 19:28:39 GMT]]></title><description><![CDATA[<p>How about iterators? Seems simpler and more flexible to me.</p>
<pre><code class="language-cpp">template &lt;typename InputIterator&gt;
void ShowArrayA( InputIterator first, InputIterator last )
{
  for ( ; first != last; ++first)
    std::cout &lt;&lt; (*first)-&gt;memberA &lt;&lt; ' ';
  std::cout &lt;&lt; endl;
}
</code></pre>
<pre><code class="language-cpp">#include &lt;algorithm&gt;
#include &lt;iterator&gt;
#include &lt;boost/iterator/indirect_iterator.hpp&gt;

std::ostream&amp; operator&lt;&lt;( std::ostream&amp; out, _A const&amp; obj )
{
  return out &lt;&lt; obj.memberA;
}

template &lt;typename InputIterator&gt;
void ShowArrayA( InputIterator first, InputIterator last )
{
  std::copy(
    boost::make_indirect_iterator(first),
    boost::make_indirect_iterator(last),
    std::ostream_iterator&lt;_A&gt;(std::cout, &quot; &quot;));
  std::cout &lt;&lt; std::endl;
}
</code></pre>
<pre><code class="language-cpp">// test

ShowArrayA(ArrayAOldStyle, ArrayAOldStyle + numObjects);
ShowArrayA(ArrayANewStyle.begin(), ArrayANewStyle.end());
ShowArrayA(ArrayBOldStyle, ArrayBOldStyle + numObjects);
ShowArrayA(ArrayBNewStyle.begin(), ArrayBNewStyle.end());
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1283698</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1283698</guid><dc:creator><![CDATA[finix]]></dc:creator><pubDate>Sat, 12 May 2007 19:28:39 GMT</pubDate></item></channel></rss>