<?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[Fehler in &amp;quot;How not to program in c++&amp;quot;?]]></title><description><![CDATA[<p>Ich behaupte, dass die Erklärung für den Fehler falsch ist, das einzige was hier nicht definiert ist, ist die Ausgabe vom d'tor, da cout ja durch nifty-initialization bei der ersten Verwendung initialisiert wird.</p>
<blockquote>
<p>A student of C++ wanted to see how constructors and destructors were called, so he wrote the following program. Yet he learned more than he bargained for. What's the problem?</p>
<p>1 /************************************************<br />
2 * Class tester. Test constructor / destructor*<br />
3 * calling. *<br />
4 <strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong>/<br />
5 #include &lt;iostream&gt;<br />
6<br />
7 /</strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong><br />
8 * tester -- Class that tells the world when *<br />
9 * it's created and destroyed. *<br />
10 ************************************************/<br />
11 class tester {<br />
12 public:<br />
13 tester(void) {<br />
14 std::cout &lt;&lt;<br />
15 &quot;tester::tester() called\n&quot;;<br />
16 }<br />
17 ~tester(void) {<br />
18 std::cout &lt;&lt;<br />
19 &quot;tester::~tester() called\n&quot;;<br />
20 }<br />
21 };<br />
22<br />
23 static tester a_var; // Variable to test with<br />
24<br />
25 int main()<br />
26 {<br />
27 std::cout &lt;&lt; &quot;In main\n&quot;;<br />
28 return (0);<br />
29 }</p>
</blockquote>
<blockquote>
<p>The problem is that the initialization order of global variable is not guaranteed. In this case, a_var assumes that std::cout is initialized. That may not be the case.</p>
<p>Let's assume the worse and assume that the initialization order is a_var, std::cout. In that case, a_var is created. The constructor is called and output a message to std::cout. Because std::cout has not been created yet, things get very confused and the program crashes.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/topic/159645/fehler-in-quot-how-not-to-program-in-c-quot</link><generator>RSS for Node</generator><lastBuildDate>Thu, 10 Sep 2026 22:03:01 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/159645.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 17 Sep 2006 15:07:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Fehler in &amp;quot;How not to program in c++&amp;quot;? on Sun, 17 Sep 2006 15:07:24 GMT]]></title><description><![CDATA[<p>Ich behaupte, dass die Erklärung für den Fehler falsch ist, das einzige was hier nicht definiert ist, ist die Ausgabe vom d'tor, da cout ja durch nifty-initialization bei der ersten Verwendung initialisiert wird.</p>
<blockquote>
<p>A student of C++ wanted to see how constructors and destructors were called, so he wrote the following program. Yet he learned more than he bargained for. What's the problem?</p>
<p>1 /************************************************<br />
2 * Class tester. Test constructor / destructor*<br />
3 * calling. *<br />
4 <strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong>/<br />
5 #include &lt;iostream&gt;<br />
6<br />
7 /</strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong><br />
8 * tester -- Class that tells the world when *<br />
9 * it's created and destroyed. *<br />
10 ************************************************/<br />
11 class tester {<br />
12 public:<br />
13 tester(void) {<br />
14 std::cout &lt;&lt;<br />
15 &quot;tester::tester() called\n&quot;;<br />
16 }<br />
17 ~tester(void) {<br />
18 std::cout &lt;&lt;<br />
19 &quot;tester::~tester() called\n&quot;;<br />
20 }<br />
21 };<br />
22<br />
23 static tester a_var; // Variable to test with<br />
24<br />
25 int main()<br />
26 {<br />
27 std::cout &lt;&lt; &quot;In main\n&quot;;<br />
28 return (0);<br />
29 }</p>
</blockquote>
<blockquote>
<p>The problem is that the initialization order of global variable is not guaranteed. In this case, a_var assumes that std::cout is initialized. That may not be the case.</p>
<p>Let's assume the worse and assume that the initialization order is a_var, std::cout. In that case, a_var is created. The constructor is called and output a message to std::cout. Because std::cout has not been created yet, things get very confused and the program crashes.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/1139059</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1139059</guid><dc:creator><![CDATA[Korrektor]]></dc:creator><pubDate>Sun, 17 Sep 2006 15:07:24 GMT</pubDate></item><item><title><![CDATA[Reply to Fehler in &amp;quot;How not to program in c++&amp;quot;? on Sun, 17 Sep 2006 15:12:09 GMT]]></title><description><![CDATA[<p>Korrektor schrieb:</p>
<blockquote>
<p>Ich behaupte, dass die Erklärung für den Fehler falsch ist, das einzige was hier nicht definiert ist, ist die Ausgabe vom d'tor, da cout ja durch nifty-initialization bei der ersten Verwendung initialisiert wird.</p>
</blockquote>
<p>wenn der ctor von cout vorher ist, dann ist auch der dtor von cout nachher. die zu löschenden objekte liegen nämliche auf nem stack (als verkettete liste vermutlich).</p>
<p>damit ist da gar kein fehler.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1139061</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1139061</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 17 Sep 2006 15:12:09 GMT</pubDate></item><item><title><![CDATA[Reply to Fehler in &amp;quot;How not to program in c++&amp;quot;? on Sun, 17 Sep 2006 15:17:21 GMT]]></title><description><![CDATA[<p>sein stil ist ungewöhnlich, finde ich</p>
<pre><code class="language-cpp">1 /************************************************ 
2 * Class tester. Test constructor / destructor* 
3 * calling. * 
4 ************************************************/ 
//naja, wer's braucht

5 #include &lt;iostream&gt; 
//warum kein usine namespace std;?

6 
7 /************************************************ 
8 * tester -- Class that tells the world when * 
9 * it's created and destroyed. * 
10 ************************************************/ 
//hat er schonmal gesagt

11 class tester { 
12 public: 
//struct?

13 tester(void) { 
//(void) ist aber voll veraltet

14 std::cout &lt;&lt; 
15 &quot;tester::tester() called\n&quot;; 
16 } 
17 ~tester(void) { 
//dito

18 std::cout &lt;&lt; 
19 &quot;tester::~tester() called\n&quot;; 
20 } 
21 }; 
22 

//ist deprecated
23 static tester a_var; // Variable to test with 
24 
25 int main() 
26 { 
27 std::cout &lt;&lt; &quot;In main\n&quot;; 

//return ist keine funktion und war nie eine. weg mit den klammern
28 return (0); 
29 }
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1139064</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1139064</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Sun, 17 Sep 2006 15:17:21 GMT</pubDate></item><item><title><![CDATA[Reply to Fehler in &amp;quot;How not to program in c++&amp;quot;? on Sun, 17 Sep 2006 15:25:06 GMT]]></title><description><![CDATA[<p>Korrektor schrieb:</p>
<blockquote>
<p>Ich behaupte, dass die Erklärung für den Fehler falsch ist, das einzige was hier nicht definiert ist, ist die Ausgabe vom d'tor, da cout ja durch nifty-initialization bei der ersten Verwendung initialisiert wird.</p>
</blockquote>
<p>wir wissen nicht wirklich wie cout konstruiert wird. der standard garantiert aber glücklicherweise, dass die lebenszeit der standard-iostream-objekte hinreichend lang ist, um in konstruktoren und destruktoren statischer objekte verwendet werden zu können (27.3./2 und Fußnote 265). das verhalten ist hier also tatsächlich wohldefiniert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1139067</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1139067</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sun, 17 Sep 2006 15:25:06 GMT</pubDate></item><item><title><![CDATA[Reply to Fehler in &amp;quot;How not to program in c++&amp;quot;? on Mon, 18 Sep 2006 07:08:45 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>//warum kein usine namespace std;?</p>
</blockquote>
<p>Weil Du damit den Sinn von Namespaces wieder aushebelst?<br />
Ok bei solch einfachen Beispielen kein Problem, aber lass das nicht zur Gewohnheit werden in grösseren Projekten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1139338</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1139338</guid><dc:creator><![CDATA[THX 1138]]></dc:creator><pubDate>Mon, 18 Sep 2006 07:08:45 GMT</pubDate></item><item><title><![CDATA[Reply to Fehler in &amp;quot;How not to program in c++&amp;quot;? on Mon, 18 Sep 2006 09:51:06 GMT]]></title><description><![CDATA[<p>1310-Logik schrieb:</p>
<blockquote>
<p>volkard schrieb:</p>
<blockquote>
<p>//warum kein usine namespace std;?</p>
</blockquote>
<p>Weil Du damit den Sinn von Namespaces wieder aushebelst?</p>
</blockquote>
<p>Blödsinn. Der Sinn von Namepsaces ist nicht dich zur Eingaben von Präfixen zu zwingen und damit das Karpaltunnelsyndrom schneller zu verbreiten. Namespaces helfen Entitäten zu gruppieren und Namenskonflikte zu vermeiden und zwar über große/verschiedene Projekte hinweg.</p>
<p>Lokal innerhalb einer cpp-Datei hat man aber volle Kontrolle über alle Namen. Man muss sich hier also nicht vor Namenskonflikten fürchten. Insofern kann man hier auch problemlos alle Namen eines Namespaces über eine using-Direktive verfügbar machen und sich die Präfixe sparen. Das ist einfach eine Frage des persönliches Gechmacks. Diese Form der Bequemlichkeit findet in der Sprache an einigen Stellen sogar ohne using-Dirketiven/Deklarationen statt - siehe ADL. Argument wie: &quot; using-Direktiven führen dazu, dass ich nicht mehr bei jeder Funktion weiß, aus welchem Namespace sie kommt&quot; gelten also nicht, da dies auch für Code ohne usings gilt.</p>
<p>Das Gesagte gilt natürlich nur für usings innerhalb von cpp-Dateien nach allen includes.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1139428</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1139428</guid><dc:creator><![CDATA[HumeSikkins]]></dc:creator><pubDate>Mon, 18 Sep 2006 09:51:06 GMT</pubDate></item></channel></rss>