<?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[Überladung des Operators ++]]></title><description><![CDATA[<p>Hallo,</p>
<p>folgendes Programm:</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;

struct Punkt {
	int x, y; //öffentlich

public:

	Punkt(int xx = 0, int yy = 0) {
		x = xx;
		y = yy;
	}
	void show(void) {
		cout &lt;&lt; '(' &lt;&lt; x &lt;&lt; ',' &lt;&lt; y &lt;&lt; ')' &lt;&lt; endl;
	}
	Punkt operator++();
};

Punkt Punkt::operator++() {
	return Punkt(x++, y++);
}

int main() {
	Punkt p1, p2;
	++p1;
	p1.show();
	p2 = ++p1;
	p1.show();
	p2.show();
	return 0;
}
</code></pre>
<p>Wieso wird am Ende (1,1) ausgegeben? Es findet doch eine Zuweisung statt p2 = ++p1;</p>
<p>Vielen Dank!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/327014/überladung-des-operators</link><generator>RSS for Node</generator><lastBuildDate>Thu, 09 Jul 2026 21:28:07 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/327014.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 19 Jul 2014 12:44:22 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Überladung des Operators ++ on Sat, 19 Jul 2014 12:44:22 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>folgendes Programm:</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;

struct Punkt {
	int x, y; //öffentlich

public:

	Punkt(int xx = 0, int yy = 0) {
		x = xx;
		y = yy;
	}
	void show(void) {
		cout &lt;&lt; '(' &lt;&lt; x &lt;&lt; ',' &lt;&lt; y &lt;&lt; ')' &lt;&lt; endl;
	}
	Punkt operator++();
};

Punkt Punkt::operator++() {
	return Punkt(x++, y++);
}

int main() {
	Punkt p1, p2;
	++p1;
	p1.show();
	p2 = ++p1;
	p1.show();
	p2.show();
	return 0;
}
</code></pre>
<p>Wieso wird am Ende (1,1) ausgegeben? Es findet doch eine Zuweisung statt p2 = ++p1;</p>
<p>Vielen Dank!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2409392</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2409392</guid><dc:creator><![CDATA[gregg]]></dc:creator><pubDate>Sat, 19 Jul 2014 12:44:22 GMT</pubDate></item><item><title><![CDATA[Reply to Überladung des Operators ++ on Sat, 19 Jul 2014 12:56:23 GMT]]></title><description><![CDATA[<p>Weil dein Prefix-++ das macht, was ein Postfix-++ machen sollte. Ein Prefix-++ sollte immer so aussehen:</p>
<pre><code class="language-cpp">Punkt&amp; operator++() {
  // Objekt verändern

  return *this;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2409393</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2409393</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Sat, 19 Jul 2014 12:56:23 GMT</pubDate></item><item><title><![CDATA[Reply to Überladung des Operators ++ on Sat, 19 Jul 2014 12:56:32 GMT]]></title><description><![CDATA[<p>Wenn du da auch (2,2) stehen haben willst, solltest du auch ++x und ++y benutzen, nicht postfix++.</p>
<p>Außerdem sollte der prefix++ operator eine Referenz zurückgeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2409394</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2409394</guid><dc:creator><![CDATA[oenone]]></dc:creator><pubDate>Sat, 19 Jul 2014 12:56:32 GMT</pubDate></item><item><title><![CDATA[Reply to Überladung des Operators ++ on Sat, 19 Jul 2014 13:09:56 GMT]]></title><description><![CDATA[<p>Okay, danke euch Beiden</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2409397</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2409397</guid><dc:creator><![CDATA[gregg]]></dc:creator><pubDate>Sat, 19 Jul 2014 13:09:56 GMT</pubDate></item><item><title><![CDATA[Reply to Überladung des Operators ++ on Sat, 19 Jul 2014 13:10:00 GMT]]></title><description><![CDATA[<pre><code>#include &lt;iostream&gt;

struct Punkt
{

    int x, y;

    Punkt(int xx = 0, int yy = 0) : x(xx), y(yy) //so werden die Variablen mit den Werten initialisiert
    {
    }

    void show() //leere Funktionen brauchen kein (void)
    {
        std::cout &lt;&lt; '(' &lt;&lt; x &lt;&lt; ',' &lt;&lt; y &lt;&lt; ')' &lt;&lt; &quot;\n&quot;; //hier brauchst du kein endl, weil es auch einen flush ausführt
    }

    Punkt&amp; operator++(); //ohne rückgabe einer Referenz bleibt das Objekt unverändert und nur eine Kopie mit der Value wird übergeben
};

Punkt&amp; Punkt::operator++()
{
    x++, y++;
    return *this;
}

int main()
{
    using namespace std;

    Punkt p1, p2;
    ++p1;
    p1.show(); // (1.1)
    p2 = ++p1;
    p1.show(); // (2.2)
    p2.show(); // (2.2)

}
</code></pre>
<p>Schau dir mal diesen Artikel über Operatorüberladung an: <a href="http://www.c-plusplus.net/forum/232010-full" rel="nofollow">http://www.c-plusplus.net/forum/232010-full</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2409398</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2409398</guid><dc:creator><![CDATA[cppvoid]]></dc:creator><pubDate>Sat, 19 Jul 2014 13:10:00 GMT</pubDate></item></channel></rss>