<?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[Konstruktor und Array]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe einen Konstruktor, zB. Test(string name).</p>
<p>Jetzt möchte ich davon eine Array anlegen.</p>
<p>Test *test = new Test[5];</p>
<p>Mein Problem ist das ja kein Standardkonstruktor verfügbar ist.</p>
<p>Wie kann ich für das Feld den Platz reservieren aber trotzdem keinen string name festlegen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/206247/konstruktor-und-array</link><generator>RSS for Node</generator><lastBuildDate>Sat, 10 Oct 2026 08:48:53 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/206247.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 22 Feb 2008 15:23:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Konstruktor und Array on Fri, 22 Feb 2008 15:23:31 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe einen Konstruktor, zB. Test(string name).</p>
<p>Jetzt möchte ich davon eine Array anlegen.</p>
<p>Test *test = new Test[5];</p>
<p>Mein Problem ist das ja kein Standardkonstruktor verfügbar ist.</p>
<p>Wie kann ich für das Feld den Platz reservieren aber trotzdem keinen string name festlegen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1461038</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1461038</guid><dc:creator><![CDATA[C++ Neuling]]></dc:creator><pubDate>Fri, 22 Feb 2008 15:23:31 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor und Array on Fri, 22 Feb 2008 15:36:29 GMT]]></title><description><![CDATA[<p>Hallo</p>
<p>In C++ am besten so</p>
<pre><code class="language-cpp">#include &lt;vector&gt;
...
std::vector&lt;Test&gt; array(5, &quot;Hallo&quot;);
</code></pre>
<p>oder du gibst dem Konstruktor-Parameter noch einen Default Value</p>
<pre><code class="language-cpp">Test(string name = &quot;&quot;)
</code></pre>
<p>bis bald<br />
akari</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1461050</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1461050</guid><dc:creator><![CDATA[akari]]></dc:creator><pubDate>Fri, 22 Feb 2008 15:36:29 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor und Array on Fri, 22 Feb 2008 15:47:44 GMT]]></title><description><![CDATA[<p>Array mit rohem Speicher erstellen und über placement new die Objekte erstellen.</p>
<pre><code class="language-cpp">char array[sizeof(T)*x];
T* arr=(T*)array;
for(int i=0;i&lt;x;++i) {
  new(arr+i)(value);
}
</code></pre>
<p>so in etwa aus dem gedächtnis gehackt...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1461057</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1461057</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Fri, 22 Feb 2008 15:47:44 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor und Array on Fri, 22 Feb 2008 15:55:45 GMT]]></title><description><![CDATA[<p>Shade Of Mine schrieb:</p>
<blockquote>
<p>Array mit rohem Speicher erstellen und über placement new die Objekte erstellen.</p>
<pre><code class="language-cpp">char array[sizeof(T)*x];
T* arr=(T*)array;
for(int i=0;i&lt;x;++i) {
  new(arr+i)(value);
}
</code></pre>
<p>so in etwa aus dem gedächtnis gehackt...</p>
</blockquote>
<p>Hier (bei statischen oder automatischen Variablen - bei new char[..] ist das kein Problem) muss allerdings auf Alignment geachtet werden.</p>
<pre><code class="language-cpp">boost::aligned_storage&lt;sizeof(T)*x,boost:: alignment_of&lt;T&gt; &gt; array;
T* arr=(T*)array;
for(int i=0;i&lt;x;++i) {
  new(arr+i)(value);
}
</code></pre>
<p>Ich habe allerdings Zweifel, dass das eine geeignete Lösung ist. std::vector liegt näher.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1461059</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1461059</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 22 Feb 2008 15:55:45 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor und Array on Fri, 22 Feb 2008 16:26:14 GMT]]></title><description><![CDATA[<p>C++ Neuling schrieb:</p>
<blockquote>
<p>ich habe einen Konstruktor, zB. Test(string name).</p>
</blockquote>
<p>2 kleine Anmerkungen die jetzt nichts mit der Frage zu tun haben, aber sich anhand deines Konstruktorbeispieles herleiten lassen:</p>
<p>1. Niemals using namespace in einem Header verwenden!<br />
2. Objekte nicht &quot;per Value&quot; übergeben, sondern als konstante Referenz</p>
<p>Sprich statt ...<br />
Test(string name);<br />
... sollte es ...<br />
Test(const std::string&amp; name);<br />
... lauten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1461073</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1461073</guid><dc:creator><![CDATA[Kleine Anmerkung]]></dc:creator><pubDate>Fri, 22 Feb 2008 16:26:14 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor und Array on Sun, 24 Feb 2008 11:40:53 GMT]]></title><description><![CDATA[<p>Danke für die Hilfe, habe das Problem mit einem Vector gelöst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1461876</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1461876</guid><dc:creator><![CDATA[C++ Neuling]]></dc:creator><pubDate>Sun, 24 Feb 2008 11:40:53 GMT</pubDate></item></channel></rss>