<?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[Kleine Stilfrage]]></title><description><![CDATA[<p>Moin,</p>
<p>in C++ hat man ja die möglichkeit (im gegensatz zu C89) Variablen sozusagen überall zu deklarieren. Man sollte also denken dass es guter Stil ist alle Variablen in einem möglichst kleinen Gültigkeitsbereich zu deklarieren. Allerdings wird hier ja auch immer wieder der Konstruktor neu aufgerufen.<br />
zB.:</p>
<pre><code class="language-cpp">class MyTest
{
public:
  MyTest()
  {
    std::cout &lt;&lt; &quot;Konstruktor!&quot; &lt;&lt; std::endl;
  }
};

int main()
{
  for (std::size_t i = 0; i &lt; 10; ++i)
  {
    MyTest instance;
  }
  return 0;
}
</code></pre>
<p>Wenn man jetzt davon ausgeht dass die Schleife ziemlich oft durchlaufen wird kann das schon peformance einbrüche geben oder? Und was jetzt? Variable ausserhalb des &quot;for-scopes&quot; deklarieren? Oder lieber eine statische Variable?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/281702/kleine-stilfrage</link><generator>RSS for Node</generator><lastBuildDate>Sun, 23 Aug 2026 12:18:52 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/281702.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 06 Feb 2011 14:13:47 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Kleine Stilfrage on Sun, 06 Feb 2011 14:13:47 GMT]]></title><description><![CDATA[<p>Moin,</p>
<p>in C++ hat man ja die möglichkeit (im gegensatz zu C89) Variablen sozusagen überall zu deklarieren. Man sollte also denken dass es guter Stil ist alle Variablen in einem möglichst kleinen Gültigkeitsbereich zu deklarieren. Allerdings wird hier ja auch immer wieder der Konstruktor neu aufgerufen.<br />
zB.:</p>
<pre><code class="language-cpp">class MyTest
{
public:
  MyTest()
  {
    std::cout &lt;&lt; &quot;Konstruktor!&quot; &lt;&lt; std::endl;
  }
};

int main()
{
  for (std::size_t i = 0; i &lt; 10; ++i)
  {
    MyTest instance;
  }
  return 0;
}
</code></pre>
<p>Wenn man jetzt davon ausgeht dass die Schleife ziemlich oft durchlaufen wird kann das schon peformance einbrüche geben oder? Und was jetzt? Variable ausserhalb des &quot;for-scopes&quot; deklarieren? Oder lieber eine statische Variable?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2017340</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017340</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Sun, 06 Feb 2011 14:13:47 GMT</pubDate></item><item><title><![CDATA[Reply to Kleine Stilfrage on Sun, 06 Feb 2011 14:17:47 GMT]]></title><description><![CDATA[<p>Es hängt davon ab, was die Klasse leistet. Ist sie sehr klein, dann sollte das nicht beachtenswert sein. Ist die Klasse sehr groß, dann <strong>kann</strong> das ins Gewicht fallen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2017342</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017342</guid><dc:creator><![CDATA[jghjghj]]></dc:creator><pubDate>Sun, 06 Feb 2011 14:17:47 GMT</pubDate></item><item><title><![CDATA[Reply to Kleine Stilfrage on Sun, 06 Feb 2011 14:27:55 GMT]]></title><description><![CDATA[<p>Die Frage ist sinnfrei. Wo man die Variable deklariert, ergibt sich doch daraus wie man die Variable verwendet. Und dann stellt sich die Frage garnicht mehr.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2017343</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017343</guid><dc:creator><![CDATA[nobaadi]]></dc:creator><pubDate>Sun, 06 Feb 2011 14:27:55 GMT</pubDate></item><item><title><![CDATA[Reply to Kleine Stilfrage on Sun, 06 Feb 2011 15:30:28 GMT]]></title><description><![CDATA[<p>nobaadi schrieb:</p>
<blockquote>
<p>Die Frage ist sinnfrei. Wo man die Variable deklariert, ergibt sich doch daraus wie man die Variable verwendet. Und dann stellt sich die Frage garnicht mehr.</p>
</blockquote>
<p>hä?</p>
<pre><code class="language-cpp">int main()
{
  MyClass instance;
  for (std::size_t i = 0; i &lt; 10; ++i)
  {
    // do some cool stuff with instance
  }
}

int main()
{
  for (std::size_t i = 0; i &lt; 10; ++i)
  {
    MyClass instance;
    // do some cool stuff with instance
  }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2017363</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017363</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Sun, 06 Feb 2011 15:30:28 GMT</pubDate></item><item><title><![CDATA[Reply to Kleine Stilfrage on Sun, 06 Feb 2011 15:47:37 GMT]]></title><description><![CDATA[<p>Das mit Variablen möglichst nahe an der Verwendung zu deklarieren hängt eng mit dem RAII-Konzept zusammen. Mach dich mal drüber schlau.</p>
<p>Und natürlich muss man Objekte entsprechend einer gescheiten Verwendung deklarieren/initialisieren. In deinem Fall könnte es außerhalb sinnvoller sein, aber am besten einfach mal testen mit Profiler.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2017372</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017372</guid><dc:creator><![CDATA[HighLigerBiMBam]]></dc:creator><pubDate>Sun, 06 Feb 2011 15:47:37 GMT</pubDate></item><item><title><![CDATA[Reply to Kleine Stilfrage on Sun, 06 Feb 2011 15:50:55 GMT]]></title><description><![CDATA[<p>a) du willst 10x was mit einer Instanz machen<br />
b) du willst 10x eine neue Instanz erzeugen</p>
<p>Ja haengt davon ab was du beabsichtigst ... das ist von Fall zu Fall unterschiedlich. Deswegen kann es keine bevorzugte Loesung geben. Ob a) oder b) benutzt wird, ist keine Stilfrage!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2017373</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017373</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Sun, 06 Feb 2011 15:50:55 GMT</pubDate></item><item><title><![CDATA[Reply to Kleine Stilfrage on Sun, 06 Feb 2011 15:53:13 GMT]]></title><description><![CDATA[<p>cooky451 schrieb:</p>
<blockquote>
<p>nobaadi schrieb:</p>
<blockquote>
<p>Die Frage ist sinnfrei. Wo man die Variable deklariert, ergibt sich doch daraus wie man die Variable <strong>verwendet</strong>. Und dann stellt sich die Frage garnicht mehr.</p>
</blockquote>
<p>hä?</p>
<pre><code class="language-cpp">int main()
{
  MyClass instance;
  for (std::size_t i = 0; i &lt; 10; ++i)
  {
    // do some cool stuff with instance
  }
}

int main()
{
  for (std::size_t i = 0; i &lt; 10; ++i)
  {
    MyClass instance;
    // do some cool stuff with instance
  }
}
</code></pre>
</blockquote>
<p>Du verwendest die Variable nicht. &quot;do some cool stuff with instance&quot; ist kein realistisches Problem. Bei realistischen Problemen wird die Frage eigentlich immer vom Problem beantwortet. Das hat wenig mit Stil oder RAII zu tun.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2017376</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017376</guid><dc:creator><![CDATA[nobaadi]]></dc:creator><pubDate>Sun, 06 Feb 2011 15:53:13 GMT</pubDate></item><item><title><![CDATA[Reply to Kleine Stilfrage on Sun, 06 Feb 2011 15:54:29 GMT]]></title><description><![CDATA[<p>Auch in C kannst du Variable überall deklarieren: global für das gesamte Programm oder lokal nur für bestimmte Teile wie Funktionen oder ähnliches. Auch kannst du globale Variable bei Bedarf durch lokale Deklarationen ausser Kraft setzen. Das ist alles noch reines C. Was du machst sind Klassen (C++), die wesentlich mehr können, nämlich Datendeklarationen mit darauf aufbauender Funktionalität zu verbinden. Es macht wenig Sinn, Klassen nur für einzelne Daten ohne jede weitere Funktionalität zu schaffen oder diese in einer for-Schleife ohne erkennbare Unterscheidung der gebildeten Instanzen einzusetzen.</p>
<p>Du hast nach Stil gefragt. Ein solcher Stil wäre z.B class HUND (....); und daraus klar erkennbar gebildete Instanzen wie HUND Dackel; HUND Mops; HUND Pudel; etc.</p>
<p>Zur Vorbeugung: Das wäre C mit Klassen. Die C++ Puristen mögen sich weiter austoben, wenn es uns denn etwas lernt!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2017377</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017377</guid><dc:creator><![CDATA[berniebutt]]></dc:creator><pubDate>Sun, 06 Feb 2011 15:54:29 GMT</pubDate></item><item><title><![CDATA[Reply to Kleine Stilfrage on Sun, 06 Feb 2011 16:45:25 GMT]]></title><description><![CDATA[<p>berniebutt schrieb:</p>
<blockquote>
<p>Du hast nach Stil gefragt. Ein solcher Stil wäre z.B class HUND (....); und daraus klar erkennbar gebildete Instanzen wie HUND Dackel; HUND Mops; HUND Pudel; etc.</p>
<p>Zur Vorbeugung: Das wäre C mit Klassen. Die C++ Puristen mögen sich weiter austoben, wenn es uns denn etwas lernt!</p>
</blockquote>
<p>Versteht das einer?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2017406</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017406</guid><dc:creator><![CDATA[sinn???]]></dc:creator><pubDate>Sun, 06 Feb 2011 16:45:25 GMT</pubDate></item><item><title><![CDATA[Reply to Kleine Stilfrage on Sun, 06 Feb 2011 17:22:54 GMT]]></title><description><![CDATA[<p>Jenachdem wie komplex die Klassen sind, könnte sowas schneller sein:</p>
<pre><code class="language-cpp">int main()
{
  MyClass instance;
  for (std::size_t i = 0; i &lt; 10; ++i)
  {
    // do some cool stuff with instance
          ....
    // do some cool stuff with instance
    instance.putBackIntoFreshlyConstructedState();
  }
}
</code></pre>
<p>Andererseits hängt das davon ab wie komplex das <code>putBackIntoFreshlyConstructedState();</code> ist ... ich würde mich aber erst um sowas kümmern wenn da ein Performance bottleneck identifiziert wurde. Und wenn, dann eher so:</p>
<pre><code class="language-cpp">int main()
{
  {
    MyClass instance;
    for (std::size_t i = 0; i &lt; 10; ++i)
    {
      // do some cool stuff with instance
            ....
      // do some cool stuff with instance
      instance.putBackIntoFreshlyConstructedState();
    }
  }
}
</code></pre>
<p>damit die MyClass dann auch aufgeräumt ist wenn ich sie sonst nur in der Schleife brauchen würde ... sonst hängt die da im Programm rum obwohl ich sie nimmer brauche!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2017421</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017421</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Sun, 06 Feb 2011 17:22:54 GMT</pubDate></item><item><title><![CDATA[Reply to Kleine Stilfrage on Sun, 06 Feb 2011 17:51:00 GMT]]></title><description><![CDATA[<p>padreigh schrieb:</p>
<blockquote>
<p>Andererseits hängt das davon ab wie komplex das <code>putBackIntoFreshlyConstructedState();</code> ist ...</p>
</blockquote>
<p>Genau. Meist ist das nämlich gerade die Aufgabe des Konstruktors.</p>
<p>padreigh schrieb:</p>
<blockquote>
<pre><code class="language-cpp">int main()
{
  {
    // ...
  }
}
</code></pre>
</blockquote>
<p>Die inneren geschweiften Klammern sind unnötig. Der Scope wird dadurch nicht eingeschränkt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2017432</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017432</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sun, 06 Feb 2011 17:51:00 GMT</pubDate></item><item><title><![CDATA[Reply to Kleine Stilfrage on Mon, 07 Feb 2011 00:25:04 GMT]]></title><description><![CDATA[<p>Ich würde sagen: grundsätzlich in der Schleife.</p>
<p>In bestimmten Fällen macht die &quot;putBackIntoFreshlyConstructedState&quot; Variante allerdings schon Sinn. Oft muss sich das Objekt zu Beginn eines Schleifendurchlaufs auch nicht in exakt dem selben Zustand befinden wie ein frisch konstruiertes.</p>
<p>Angenommen man braucht innerhalb der Schleife einen std::vector. Den jedes mal zu zerstören und neu anzulegen ist bloss Performance-Verschwendung. <code>clear()</code> aufzurufen um den Vektor in einen &quot;ausreichend frischen&quot; Zustand zu bringen, kann da schon Sinn machen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2017614</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017614</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 07 Feb 2011 00:25:04 GMT</pubDate></item><item><title><![CDATA[Reply to Kleine Stilfrage on Mon, 07 Feb 2011 11:14:55 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>padreigh schrieb:</p>
<blockquote>
<p>Andererseits hängt das davon ab wie komplex das <code>putBackIntoFreshlyConstructedState();</code> ist ...</p>
</blockquote>
<p>Genau. Meist ist das nämlich gerade die Aufgabe des Konstruktors.</p>
<p>padreigh schrieb:</p>
<blockquote>
<pre><code class="language-cpp">int main()
{
  {
    // ...
  }
}
</code></pre>
</blockquote>
<p>Die inneren geschweiften Klammern sind unnötig. Der Scope wird dadurch nicht eingeschränkt.</p>
</blockquote>
<p>Bitte was? Dann kompilier das mal und lass es laufen ....</p>
<pre><code>int main(void)
{
    int i;
    {
        for (i=0;i&lt; 10; ++i) std::cout &lt;&lt; &quot; hallo&quot;;
    }
    std::cout &lt;&lt; i &lt;&lt; std::endl;

    {
        int j;
        {
            for (j=0;j&lt; 10; ++j) std::cout &lt;&lt; &quot; hallo&quot;;
        }
    }
    std::cout &lt;&lt; j &lt;&lt; std::endl;
}
</code></pre>
<p><code>j</code> ist da definitiv nicht mehr vorhanden ... und wenn <code>j</code> ein so dickes Ding ist, das es sich lohnt es VOR der Schleife bekannt zu machen und IN der Schleife wieder auf Startzustand zu stellen, dann würde ich meinen das es sich auch lohnt dieses NACH der Schleife nicht mehr rungeistern zu haben -</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2017739</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017739</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Mon, 07 Feb 2011 11:14:55 GMT</pubDate></item><item><title><![CDATA[Reply to Kleine Stilfrage on Mon, 07 Feb 2011 11:21:50 GMT]]></title><description><![CDATA[<p>Schau dir nochmals den Codeteil an, den ich zitiert habe. Die doppelten geschweiften Klammern um alles sind sinnlos.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2017744</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017744</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Mon, 07 Feb 2011 11:21:50 GMT</pubDate></item><item><title><![CDATA[Reply to Kleine Stilfrage on Mon, 07 Feb 2011 17:53:49 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p>Angenommen man braucht innerhalb der Schleife einen std::vector. Den jedes mal zu zerstören und neu anzulegen ist bloss Performance-Verschwendung. <code>clear()</code> aufzurufen um den Vektor in einen &quot;ausreichend frischen&quot; Zustand zu bringen, kann da schon Sinn machen.</p>
</blockquote>
<p>Wichtig dabei das KANN.<br />
Denn es kann mit clear ja auch langsamer sein...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2017926</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017926</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Mon, 07 Feb 2011 17:53:49 GMT</pubDate></item><item><title><![CDATA[Reply to Kleine Stilfrage on Mon, 07 Feb 2011 20:31:53 GMT]]></title><description><![CDATA[<p>Shade Of Mine schrieb:</p>
<blockquote>
<p>hustbaer schrieb:</p>
<blockquote>
<p>Angenommen man braucht innerhalb der Schleife einen std::vector. Den jedes mal zu zerstören und neu anzulegen ist bloss Performance-Verschwendung. <code>clear()</code> aufzurufen um den Vektor in einen &quot;ausreichend frischen&quot; Zustand zu bringen, kann da schon Sinn machen.</p>
</blockquote>
<p>Wichtig dabei das KANN.<br />
Denn es kann mit clear ja auch langsamer sein...</p>
</blockquote>
<p>Theoretisch: ja. Praktisch: kaum.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2017976</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017976</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 07 Feb 2011 20:31:53 GMT</pubDate></item><item><title><![CDATA[Reply to Kleine Stilfrage on Mon, 07 Feb 2011 20:37:37 GMT]]></title><description><![CDATA[<p>Wenn man allerdings ein &quot;Reset&quot; mit <code>operator=</code> durchführt, kann es schnell einmal langsamer sein als Destruktion/Neukonstruktion. Alleine schon weil Copy-and-Swap-Implementierungen Letzteres immer tun.</p>
<p>Das nur als Ergänzung, weil ich auch schon gesehen habe, dass ein Objekt aus Performancegründen mit <code>operator=</code> wiederverwendet wird <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/2017977</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2017977</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Mon, 07 Feb 2011 20:37:37 GMT</pubDate></item><item><title><![CDATA[Reply to Kleine Stilfrage on Mon, 07 Feb 2011 22:08:27 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>Schau dir nochmals den Codeteil an, den ich zitiert habe. Die doppelten geschweiften Klammern um alles sind sinnlos.</p>
</blockquote>
<p>Wir reden wohl aneinander vorbei. Ich hätte es wohl so formulieren sollen:</p>
<pre><code class="language-cpp">int main()
{
  // do something here
  {
    MyClass instance;
    for (std::size_t i = 0; i &lt; 10; ++i)
    {
      // do some cool stuff with instance
            ....
      // do some cool stuff with instance
      instance.putBackIntoFreshlyConstructedState();
    }
  }
  // do more here
}
</code></pre>
<p>vs.</p>
<pre><code class="language-cpp">int main()
{
  // do something here
  MyClass instance;
  for (std::size_t i = 0; i &lt; 10; ++i)
  {
    // do some cool stuff with instance
          ....
    // do some cool stuff with instance
    instance.putBackIntoFreshlyConstructedState();
  }
  // do more here
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2018021</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2018021</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Mon, 07 Feb 2011 22:08:27 GMT</pubDate></item></channel></rss>