<?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[Variable in if definieren]]></title><description><![CDATA[<p>Hallo!</p>
<p>Warum geht das hier nur mit einfachen Datentypen, nicht aber zB. mit iteratoren?</p>
<pre><code class="language-cpp">// Möchte Variable gleich im if definieren...
if(unsigned a = GetValue())
	cout &lt;&lt; &quot;a ist nicht null, sondern: &quot; &lt;&lt; a &lt;&lt; endl; // ...und hier weiteren Aufruf von GetValue() sparen, besonders wenn man a noch mehrmals braucht
else
	cout &lt;&lt; &quot;a ist null&quot; &lt;&lt; endl;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/228099/variable-in-if-definieren</link><generator>RSS for Node</generator><lastBuildDate>Tue, 29 Sep 2026 17:29:27 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/228099.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 24 Nov 2008 03:16:06 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Variable in if definieren on Mon, 24 Nov 2008 03:16:06 GMT]]></title><description><![CDATA[<p>Hallo!</p>
<p>Warum geht das hier nur mit einfachen Datentypen, nicht aber zB. mit iteratoren?</p>
<pre><code class="language-cpp">// Möchte Variable gleich im if definieren...
if(unsigned a = GetValue())
	cout &lt;&lt; &quot;a ist nicht null, sondern: &quot; &lt;&lt; a &lt;&lt; endl; // ...und hier weiteren Aufruf von GetValue() sparen, besonders wenn man a noch mehrmals braucht
else
	cout &lt;&lt; &quot;a ist null&quot; &lt;&lt; endl;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1619700</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1619700</guid><dc:creator><![CDATA[Definierer]]></dc:creator><pubDate>Mon, 24 Nov 2008 03:16:06 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in if definieren on Mon, 24 Nov 2008 03:21:01 GMT]]></title><description><![CDATA[<p>Ah sorry, ich mein es zB. so:</p>
<pre><code class="language-cpp">if((std::set&lt;MyClass&gt;::iterator myObject = objects.find(123)) != objects.end())
    ...
</code></pre>
<p>Das geht nicht...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1619701</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1619701</guid><dc:creator><![CDATA[Definierer]]></dc:creator><pubDate>Mon, 24 Nov 2008 03:21:01 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in if definieren on Mon, 24 Nov 2008 07:02:31 GMT]]></title><description><![CDATA[<p>Definierer schrieb:</p>
<blockquote>
<p>Das geht nicht...</p>
</blockquote>
<p>... ist eine sehr informative Fehlermeldung...</p>
<p>mal abgesehen davon machst du nicht das gleiche: bei der ersten Verison ist die erzeugte Variable selbst die Kondition, also in kurz <code>if (a)</code> , beim zweiten Mal ist die Kondition der Vergleich der erzeugten Variablen mit einem anderen Wert. IIRC würde folgendes also auch nicht funktionieren:</p>
<pre><code class="language-cpp">if ((unsigned a = GetValue()) != 1)
</code></pre>
<p>ein Workaround wäre folgendes:</p>
<pre><code class="language-cpp">{
  std::set&lt;MyClass&gt;::iterator myObject = objects.find(123);
  if (myObject != objects.end())
  {
  }
  else
  {
  }
}
</code></pre>
<p>Ich meine mal gelesen zu haben, dass der Komma-Operator in if-Konditionen nicht erlaubt ist, falls doch, wäre folgendes auch eine Möglichkeit:</p>
<pre><code class="language-cpp">if (std::set&lt;MyClass&gt;::iterator myObject(objects.find(123)), myObject != objects.end())
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1619715</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1619715</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Mon, 24 Nov 2008 07:02:31 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in if definieren on Mon, 24 Nov 2008 09:16:39 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>Ich meine mal gelesen zu haben, dass der Komma-Operator in if-Konditionen nicht erlaubt ist, falls doch, wäre folgendes auch eine Möglichkeit:</p>
<pre><code class="language-cpp">if (std::set&lt;MyClass&gt;::iterator myObject(objects.find(123)), myObject != objects.end())
</code></pre>
</blockquote>
<p>Komma-Operator geht. Der rechte Teil wird ausgewertet. Allerdings kannst du kein Objekt im if statement erzeugen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1619767</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1619767</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Mon, 24 Nov 2008 09:16:39 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in if definieren on Mon, 24 Nov 2008 09:23:29 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>...IIRC würde folgendes also auch nicht funktionieren:</p>
<pre><code class="language-cpp">if ((unsigned a = GetValue()) != 1)
</code></pre>
<p>...</p>
</blockquote>
<p>Das Ergebnis einer Deklaration einer benannten Variable scheint ja nicht die Variable selbst zu sein.<br />
Wobei mich das schon ein wenig verwundert, weil das bei &quot;unbenannten Objekten&quot; funktioniert.</p>
<pre><code class="language-cpp">struct B {
   int i;
   B() : i(3) {}
   operator int() { return i; }
};

int main() {
   if(B() == 3) cout &lt;&lt; &quot;if1\n&quot;; // OK
   if((B b) == 3) cout &lt;&lt; &quot;if2\n&quot;; // &quot;main.cpp&quot;, line 40.10: 1540-0063 (S) The text &quot;b&quot; is unexpected.

   return 0;
}
</code></pre>
<p>(xlC V.5)</p>
<p>drakon schrieb:</p>
<blockquote>
<p>...Allerdings kannst du kein Objekt im if statement erzeugen.</p>
</blockquote>
<p>hmmmm - steht das im Std irgendwo?<br />
Implizit machen wir das doch überall:</p>
<pre><code class="language-cpp">if(atoi(&quot;2&quot;) == 2) ...
</code></pre>
<p>erzeugt doch auch ein int im if ... und ich bin ziemlich sicher, dass das mit std::sting und allen andern Objekten auch &quot;geht&quot;...<br />
Mein Verdacht halt: Sobald ein &quot;Name&quot; ins Spiel kommt, weiß der Compiler nicht, was er damit machen soll...</p>
<p>Gruß,</p>
<p>Simon2.</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1619769</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1619769</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Mon, 24 Nov 2008 09:23:29 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in if definieren on Mon, 24 Nov 2008 09:28:18 GMT]]></title><description><![CDATA[<p>Simon2 schrieb:</p>
<blockquote>
<p>pumuckl schrieb:</p>
<blockquote>
<p>...IIRC würde folgendes also auch nicht funktionieren:</p>
<pre><code class="language-cpp">if ((unsigned a = GetValue()) != 1)
</code></pre>
<p>...</p>
</blockquote>
<p>Das Ergebnis einer Deklaration einer benannten Variable scheint ja nicht die Variable selbst zu sein.<br />
Wobei mich das schon ein wenig verwundert, weil das bei &quot;unbenannten Objekten&quot; funktioniert.</p>
</blockquote>
<p>B() ist ein temporäres Objekt, während B b eine Dekleration ist..</p>
<blockquote>
<p>erzeugt doch auch ein int im if ... und ich bin ziemlich sicher, dass das mit std::sting und allen andern Objekten auch &quot;geht&quot;...<br />
Mein Verdacht halt: Sobald ein &quot;Name&quot; ins Spiel kommt, weiß der Compiler nicht, was er damit machen soll...</p>
</blockquote>
<p>Ja. Erzeugung von tempoären Objekten ist erlaubt.</p>
<p>(Siehe 12.2)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1619772</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1619772</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Mon, 24 Nov 2008 09:28:18 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in if definieren on Mon, 24 Nov 2008 09:32:47 GMT]]></title><description><![CDATA[<p>drakon schrieb:</p>
<blockquote>
<p>...B() ist ein temporäres Objekt, während B b eine Dekleration ist...</p>
</blockquote>
<p>Ich hatte das statement &quot;B()&quot; in dem Zusammenhang immer als &quot;Deklaration eines temporären und unbenannten Objekts&quot; aufgefasst.</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1619774</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1619774</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Mon, 24 Nov 2008 09:32:47 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in if definieren on Mon, 24 Nov 2008 09:38:08 GMT]]></title><description><![CDATA[<p>B() ist ein Ausdruck. Deklarationen sind keine Ausdrücke.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1619779</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1619779</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Mon, 24 Nov 2008 09:38:08 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in if definieren on Mon, 24 Nov 2008 11:50:56 GMT]]></title><description><![CDATA[<p>Bashar schrieb:</p>
<blockquote>
<p>B() ist ein Ausdruck. Deklarationen sind keine Ausdrücke.</p>
</blockquote>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f4a1.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--light_bulb"
      title=":bulb:"
      alt="💡"
    /> Aha!</p>
<p>Darüber muss ich noch ein wenig meditieren. Ich merke gerade, dass ich mir über den Begriff &quot;Ausdruck&quot; noch nicht ausreichend Gedanken gemacht habe.</p>
<p>Danke,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1619848</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1619848</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Mon, 24 Nov 2008 11:50:56 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in if definieren on Mon, 24 Nov 2008 17:47:32 GMT]]></title><description><![CDATA[<p>Stimmt, das hier geht nicht...</p>
<pre><code class="language-cpp">if((unsigned a = GetValue()) != 0)
		cout &lt;&lt; &quot;a ist nicht null, sondern: &quot; &lt;&lt; a &lt;&lt; endl;
	else
		cout &lt;&lt; &quot;a ist null&quot; &lt;&lt; endl;
</code></pre>
<p>Find ich schon sehr schade und verstehe noch immer nicht warum das so ist.<br />
Wenn &quot;if(unsigned a = 3)&quot; gültig ist, sollte &quot;if((unsigned a = 3) != 0)&quot; es auch sein...</p>
<p>Wenn &quot;unsigned a = 3&quot; kein Ausdruck ist, warum darf es dann im ' if ' stehen?<br />
Ich habe gelernt:</p>
<p>&quot;Der Wert einer Zuweisung ist der zugewiesene Wert&quot;.</p>
<p>Demnach wäre &quot;if((unsigned a = 3) != 0)&quot; doch gleichzusetzen mit &quot;if((3) != 0)&quot;.</p>
<p>Nunja, dies ist halt auch eine Definition, aber wenn es doch kein Ausdruck ist?</p>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1620094</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1620094</guid><dc:creator><![CDATA[Definierer]]></dc:creator><pubDate>Mon, 24 Nov 2008 17:47:32 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in if definieren on Mon, 24 Nov 2008 18:02:38 GMT]]></title><description><![CDATA[<p>Es handelt sich dabei aber nicht um eine Zuweisung sondern um eine Definition:</p>
<pre><code class="language-cpp">int a = 5; //Definition
a = 6; //Zuweisung
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1620101</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1620101</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Mon, 24 Nov 2008 18:02:38 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in if definieren on Mon, 24 Nov 2008 21:49:29 GMT]]></title><description><![CDATA[<p>Ja, aber hier doch auch:</p>
<p>if(unsigned a = GetValue())<br />
...</p>
<p>Und wenn die (anscheinend) einen boolsche Wert sein kann, so sollte es hier auch so sein:</p>
<p>if((unsigned a = GetValue()) != 0)<br />
...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1620194</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1620194</guid><dc:creator><![CDATA[Definierer]]></dc:creator><pubDate>Mon, 24 Nov 2008 21:49:29 GMT</pubDate></item><item><title><![CDATA[Reply to Variable in if definieren on Mon, 24 Nov 2008 22:18:40 GMT]]></title><description><![CDATA[<p>Dass Deklarationen im Kopf von if-Anweisungen (bei while und switch m.W. auch) stehen dürfen ist ein Sonderfall. Daraus kann man nicht ableiten, dass das Ausdrücke sind, die man irgendwie anders noch verwenden könnte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1620210</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1620210</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Mon, 24 Nov 2008 22:18:40 GMT</pubDate></item></channel></rss>