<?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[lazy evaluation bei klassen instantiierung]]></title><description><![CDATA[<p>Ich stehe vor der frage dass ich nicht weiß was sauberer ist.<br />
Sollte ich immer erst dann ein Objekt einer Klasse instantiieren wenn ich es unmittelbar brauche oder sollte ich für die Übersicht alle variablen/objekte in einer methode ganz am anfang anlegen:</p>
<p>welche variante würdet ihr vorziehen und warum:</p>
<ol>
<li></li>
</ol>
<pre><code class="language-cpp">int Class::mymethod()
{
      int    a = 9,
              b,
              c,
              myvar;

     double mydouble = 9.9,
                 yourdouble;

     Class myclass;

     Class2* yourclass = new Class2();

 // ab hier ist alles instaniiert und kann benutzt werden. klare trennung zwischen 
// objekt/variablen initialisierung und dem eigentlich methoden-rumpf.
}
</code></pre>
<ol start="2">
<li></li>
</ol>
<pre><code class="language-cpp">int Class::mymethod()
{
      int    a = 9;
      for (int a = 0; a &lt; 50; a++)
      { 
          // ... 
       }

       int b;

       if ( somevariable &lt; b) //...

       Class2* yourclass = new Class2();
       yourclass-&gt;foo();
}
</code></pre>
<p>danke</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/255468/lazy-evaluation-bei-klassen-instantiierung</link><generator>RSS for Node</generator><lastBuildDate>Fri, 11 Sep 2026 21:26:48 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/255468.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 01 Dec 2009 11:03:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to lazy evaluation bei klassen instantiierung on Tue, 01 Dec 2009 11:03:14 GMT]]></title><description><![CDATA[<p>Ich stehe vor der frage dass ich nicht weiß was sauberer ist.<br />
Sollte ich immer erst dann ein Objekt einer Klasse instantiieren wenn ich es unmittelbar brauche oder sollte ich für die Übersicht alle variablen/objekte in einer methode ganz am anfang anlegen:</p>
<p>welche variante würdet ihr vorziehen und warum:</p>
<ol>
<li></li>
</ol>
<pre><code class="language-cpp">int Class::mymethod()
{
      int    a = 9,
              b,
              c,
              myvar;

     double mydouble = 9.9,
                 yourdouble;

     Class myclass;

     Class2* yourclass = new Class2();

 // ab hier ist alles instaniiert und kann benutzt werden. klare trennung zwischen 
// objekt/variablen initialisierung und dem eigentlich methoden-rumpf.
}
</code></pre>
<ol start="2">
<li></li>
</ol>
<pre><code class="language-cpp">int Class::mymethod()
{
      int    a = 9;
      for (int a = 0; a &lt; 50; a++)
      { 
          // ... 
       }

       int b;

       if ( somevariable &lt; b) //...

       Class2* yourclass = new Class2();
       yourclass-&gt;foo();
}
</code></pre>
<p>danke</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1815832</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1815832</guid><dc:creator><![CDATA[Uffob]]></dc:creator><pubDate>Tue, 01 Dec 2009 11:03:14 GMT</pubDate></item><item><title><![CDATA[Reply to lazy evaluation bei klassen instantiierung on Tue, 01 Dec 2009 11:33:13 GMT]]></title><description><![CDATA[<p>Hallo</p>
<p>Es spricht nichts gegen Variante 2. Kann mehr Übersichtlichkeit bringen, und eventuell mehr Geschwindigkeit (wenn es nämlich Fälle gibt, bei denen die Methode vorzeitig beendet wird, und damit einige Speicherbereiche auf dem Heap gar nicht gebraucht wurden)</p>
<p>bis bald<br />
akari</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1815845</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1815845</guid><dc:creator><![CDATA[akari]]></dc:creator><pubDate>Tue, 01 Dec 2009 11:33:13 GMT</pubDate></item><item><title><![CDATA[Reply to lazy evaluation bei klassen instantiierung on Tue, 01 Dec 2009 23:44:07 GMT]]></title><description><![CDATA[<p>ganz klar 2</p>
<p>objekte/variablen immer so lokal wie möglich, so spät wie möglich, und so const wie möglich machen.<br />
spart viel ärger, und macht übersichtlichere programme.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1816252</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1816252</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 01 Dec 2009 23:44:07 GMT</pubDate></item><item><title><![CDATA[Reply to lazy evaluation bei klassen instantiierung on Wed, 02 Dec 2009 12:25:45 GMT]]></title><description><![CDATA[<p>Aber das hat gar nichts mit lazy evaluation zu tun.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1816411</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1816411</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Wed, 02 Dec 2009 12:25:45 GMT</pubDate></item><item><title><![CDATA[Reply to lazy evaluation bei klassen instantiierung on Thu, 03 Dec 2009 19:15:51 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p>objekte/variablen immer so lokal wie möglich, so spät wie möglich, und so const wie möglich machen.</p>
</blockquote>
<p>Oft auch so automatisch wie möglich. Besonders wenn eine Funktion komplizierter wird (mehrere Ablaufpfade, Exceptions), kann sich RAII sehr schnell auszahlen, weil man eine Absicherung für korrekte Zerstörung und Speicherfreigabe hat. Nebenbei kommt einem oft zugute, dass sich dadurch die Codekomplexität stark reduziert, da If-Orgien hinfällig werden.</p>
<pre><code class="language-cpp">Class2* yourclass = new Class2();
</code></pre>
<p>Wenn in einer Funktion überhaupt ein lokales Objekt dynamisch angefordert werden muss (was bei mir z.B. eher selten vorkommt), sollte man die Freigabe nicht vergessen. Diese stellt in vielen Fällen zwar kein Problem dar, aber man sollte bedenken, dass man durch <code>scoped_ptr</code> &amp; Co. praktisch keine Nachteile hat.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1817294</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1817294</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Thu, 03 Dec 2009 19:15:51 GMT</pubDate></item><item><title><![CDATA[Reply to lazy evaluation bei klassen instantiierung on Fri, 04 Dec 2009 00:13:11 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>hustbaer schrieb:</p>
<blockquote>
<p>objekte/variablen immer so lokal wie möglich, so spät wie möglich, und so const wie möglich machen.</p>
</blockquote>
<p>Oft auch so automatisch wie möglich. Besonders wenn eine Funktion komplizierter wird (mehrere Ablaufpfade, Exceptions), kann sich RAII sehr schnell auszahlen, weil man eine Absicherung für korrekte Zerstörung und Speicherfreigabe hat. Nebenbei kommt einem oft zugute, dass sich dadurch die Codekomplexität stark reduziert, da If-Orgien hinfällig werden.</p>
<pre><code class="language-cpp">Class2* yourclass = new Class2();
</code></pre>
<p>Wenn in einer Funktion überhaupt ein lokales Objekt dynamisch angefordert werden muss (was bei mir z.B. eher selten vorkommt), sollte man die Freigabe nicht vergessen. Diese stellt in vielen Fällen zwar kein Problem dar, aber man sollte bedenken, dass man durch <code>scoped_ptr</code> &amp; Co. praktisch keine Nachteile hat.</p>
</blockquote>
<p>Ja, klar. Ist für mich aber ein anderes Thema.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1817489</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1817489</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Fri, 04 Dec 2009 00:13:11 GMT</pubDate></item><item><title><![CDATA[Reply to lazy evaluation bei klassen instantiierung on Fri, 04 Dec 2009 11:59:14 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p>Ja, klar. Ist für mich aber ein anderes Thema.</p>
</blockquote>
<p>Ja, das war auch eher an Uffob gerichtet. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1817775</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1817775</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Fri, 04 Dec 2009 11:59:14 GMT</pubDate></item><item><title><![CDATA[Reply to lazy evaluation bei klassen instantiierung on Fri, 04 Dec 2009 16:03:31 GMT]]></title><description><![CDATA[<p>Achso, ja, weil er da ein ziemlich sinnloses &quot;new&quot; drinnen hat, und danach auch nicht aufräumt.</p>
<p>Ja, stümmt <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/1817923</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1817923</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Fri, 04 Dec 2009 16:03:31 GMT</pubDate></item></channel></rss>