<?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[Überladen von Operatoren]]></title><description><![CDATA[<p>Gutentag,</p>
<p>ich beschäftige mich zurzeit mit den Operatoren einer Klasse und wie man die überladen kann. Ich praktiziere es an dem Beispiel eines Vector2.</p>
<p>Die Klasse schaut bis jetzt so aus:</p>
<pre><code>namespace Private
{
	template&lt;typename T&gt;
	class Vector2
	{
	private:
	protected:
	public:
		T x;
		T y;

		Vector2(T pX, T pY);
		Vector2(const Vector2&lt;T&gt;&amp; copy);
		Vector2&lt;T&gt;&amp; operator=(const Vector2&lt;T&gt;&amp; copy);

		Vector2&lt;T&gt; operator-() const;

		Vector2&lt;T&gt;&amp; operator-=(const Vector2&lt;T&gt;&amp; right);
		Vector2&lt;T&gt; operator-(const Vector2&lt;T&gt;&amp; right);
		Vector2&lt;T&gt;&amp; operator+=(const Vector2&lt;T&gt;&amp; right);
		Vector2&lt;T&gt; operator+(const Vector2&lt;T&gt;&amp; right);
		Vector2&lt;T&gt;&amp; operator*=(const Vector2&lt;T&gt;&amp; right);
		Vector2&lt;T&gt; operator*(const Vector2&lt;T&gt;&amp; right);
		Vector2&lt;T&gt;&amp; operator/=(const Vector2&lt;T&gt;&amp; right);
		Vector2&lt;T&gt; operator/(const Vector2&lt;T&gt;&amp; right);

		bool operator==(const Vector2&lt;T&gt;&amp; right);
		bool operator!=(const Vector2&lt;T&gt;&amp; right);

		Vector2&lt;T&gt;&amp; operator-=(const T&amp; right);
		Vector2&lt;T&gt; operator-(const T&amp; right);
		Vector2&lt;T&gt;&amp; operator+=(const T&amp; right);
		Vector2&lt;T&gt; operator+(const T&amp; right);
		Vector2&lt;T&gt;&amp; operator*=(const T&amp; right);
		Vector2&lt;T&gt; operator*(const T&amp; right);
		Vector2&lt;T&gt;&amp; operator/=(const T&amp; right);
		Vector2&lt;T&gt; operator/(const T&amp; right);
	};

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;::Vector2(T pX, T pY) : x(pX), y(pY) {}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;::Vector2(const Vector2&lt;T&gt;&amp; copy) : x(copy.x), y(copy.y) {}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;&amp; Vector2&lt;T&gt;::operator=(const Vector2&lt;T&gt;&amp; copy)
	{
		x = copy.x;
		y = copy.y;
		return *this;
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt; Vector2&lt;T&gt;::operator-() const
	{
		return Vector2&lt;T&gt;(-x, -y);
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;&amp; Vector2&lt;T&gt;::operator-=(const Vector2&lt;T&gt;&amp; right)
	{
		x -= right.x;
		y -= right.y;
		return *this;
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt; Vector2&lt;T&gt;::operator-(const Vector2&lt;T&gt;&amp; right)
	{
		return Vector2&lt;T&gt;(x - right.x, y - right.y);
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;&amp; Vector2&lt;T&gt;::operator+=(const Vector2&lt;T&gt;&amp; right)
	{
		x += right.x;
		y += right.y;
		return *this;
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt; Vector2&lt;T&gt;::operator+(const Vector2&lt;T&gt;&amp; right)
	{
		return Vector2&lt;T&gt;(x + right.x, y + right.y);
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;&amp; Vector2&lt;T&gt;::operator*=(const Vector2&lt;T&gt;&amp; right)
	{
		x *= right.x;
		y *= right.y;
		return *this;
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt; Vector2&lt;T&gt;::operator*(const Vector2&lt;T&gt;&amp; right)
	{
		return Vector2&lt;T&gt;(x * right.x, y * right.y);
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;&amp; Vector2&lt;T&gt;::operator/=(const Vector2&lt;T&gt;&amp; right)
	{
		x /= right.x;
		y /= right.y;
		return *this;
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt; Vector2&lt;T&gt;::operator/(const Vector2&lt;T&gt;&amp; right)
	{
		return Vector2&lt;T&gt;(x / right.x, y / right.y);
	}

	template&lt;typename T&gt;
	inline bool Vector2&lt;T&gt;::operator==(const Vector2&lt;T&gt;&amp; right)
	{
		return x == right.x ? y == right.y : false;
	}

	template&lt;typename T&gt;
	inline bool Vector2&lt;T&gt;::operator!=(const Vector2&lt;T&gt;&amp; right)
	{
		return !(*this == right);
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;&amp; Vector2&lt;T&gt;::operator-=(const T&amp; right)
	{
		x -= right;
		y -= right;
		return *this;
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt; Vector2&lt;T&gt;::operator-(const T&amp; right)
	{
		return Vector2&lt;T&gt;(x - right, y - right);
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;&amp; Vector2&lt;T&gt;::operator+=(const T&amp; right)
	{
		x += right;
		y += right;
		return *this;
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt; Vector2&lt;T&gt;::operator+(const T&amp; right)
	{
		return Vector2&lt;T&gt;(x + right, y + right);
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;&amp; Vector2&lt;T&gt;::operator*=(const T&amp; right)
	{
		x *= right;
		y *= right;
		return *this;
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt; Vector2&lt;T&gt;::operator*(const T&amp; right)
	{
		return Vector2&lt;T&gt;(x * right, y * right);
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;&amp; Vector2&lt;T&gt;::operator/=(const T&amp; right)
	{
		x /= right;
		y /= right;
		return *this;
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt; Vector2&lt;T&gt;::operator/(const T&amp; right)
	{
		return Vector2&lt;T&gt;(x / right, y / right);
	}
}
</code></pre>
<pre><code>typedef Private::Vector2&lt;int&gt; Vector2I;
</code></pre>
<p>Damit kann ich erstmal die Vektoren miteinander addieren, substrahieren, multiplizieren, etc...</p>
<p>indem ich folgendes schreibe:</p>
<pre><code>Vector2I v1(1, 1);
	Vector2I v2(2, 2);
	Vector2I v3(3, 3);

	v1 = -v2;
	v1 = v1 - v2;
	v1 = v2 - v3;
</code></pre>
<p>was mir aber noch fehlt bzw. wollte ich fragen ob es überhaupt möglich ist auch solche operationen durchzuführen/zu überladen:</p>
<pre><code>v1 - v2; // Hier wird dann v1 als Referenz geändert zurückgegeben
v1 + v2; // etc...
</code></pre>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/339108/überladen-von-operatoren</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Apr 2026 11:18:03 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/339108.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 03 Aug 2016 02:06:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Überladen von Operatoren on Wed, 03 Aug 2016 02:06:01 GMT]]></title><description><![CDATA[<p>Gutentag,</p>
<p>ich beschäftige mich zurzeit mit den Operatoren einer Klasse und wie man die überladen kann. Ich praktiziere es an dem Beispiel eines Vector2.</p>
<p>Die Klasse schaut bis jetzt so aus:</p>
<pre><code>namespace Private
{
	template&lt;typename T&gt;
	class Vector2
	{
	private:
	protected:
	public:
		T x;
		T y;

		Vector2(T pX, T pY);
		Vector2(const Vector2&lt;T&gt;&amp; copy);
		Vector2&lt;T&gt;&amp; operator=(const Vector2&lt;T&gt;&amp; copy);

		Vector2&lt;T&gt; operator-() const;

		Vector2&lt;T&gt;&amp; operator-=(const Vector2&lt;T&gt;&amp; right);
		Vector2&lt;T&gt; operator-(const Vector2&lt;T&gt;&amp; right);
		Vector2&lt;T&gt;&amp; operator+=(const Vector2&lt;T&gt;&amp; right);
		Vector2&lt;T&gt; operator+(const Vector2&lt;T&gt;&amp; right);
		Vector2&lt;T&gt;&amp; operator*=(const Vector2&lt;T&gt;&amp; right);
		Vector2&lt;T&gt; operator*(const Vector2&lt;T&gt;&amp; right);
		Vector2&lt;T&gt;&amp; operator/=(const Vector2&lt;T&gt;&amp; right);
		Vector2&lt;T&gt; operator/(const Vector2&lt;T&gt;&amp; right);

		bool operator==(const Vector2&lt;T&gt;&amp; right);
		bool operator!=(const Vector2&lt;T&gt;&amp; right);

		Vector2&lt;T&gt;&amp; operator-=(const T&amp; right);
		Vector2&lt;T&gt; operator-(const T&amp; right);
		Vector2&lt;T&gt;&amp; operator+=(const T&amp; right);
		Vector2&lt;T&gt; operator+(const T&amp; right);
		Vector2&lt;T&gt;&amp; operator*=(const T&amp; right);
		Vector2&lt;T&gt; operator*(const T&amp; right);
		Vector2&lt;T&gt;&amp; operator/=(const T&amp; right);
		Vector2&lt;T&gt; operator/(const T&amp; right);
	};

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;::Vector2(T pX, T pY) : x(pX), y(pY) {}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;::Vector2(const Vector2&lt;T&gt;&amp; copy) : x(copy.x), y(copy.y) {}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;&amp; Vector2&lt;T&gt;::operator=(const Vector2&lt;T&gt;&amp; copy)
	{
		x = copy.x;
		y = copy.y;
		return *this;
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt; Vector2&lt;T&gt;::operator-() const
	{
		return Vector2&lt;T&gt;(-x, -y);
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;&amp; Vector2&lt;T&gt;::operator-=(const Vector2&lt;T&gt;&amp; right)
	{
		x -= right.x;
		y -= right.y;
		return *this;
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt; Vector2&lt;T&gt;::operator-(const Vector2&lt;T&gt;&amp; right)
	{
		return Vector2&lt;T&gt;(x - right.x, y - right.y);
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;&amp; Vector2&lt;T&gt;::operator+=(const Vector2&lt;T&gt;&amp; right)
	{
		x += right.x;
		y += right.y;
		return *this;
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt; Vector2&lt;T&gt;::operator+(const Vector2&lt;T&gt;&amp; right)
	{
		return Vector2&lt;T&gt;(x + right.x, y + right.y);
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;&amp; Vector2&lt;T&gt;::operator*=(const Vector2&lt;T&gt;&amp; right)
	{
		x *= right.x;
		y *= right.y;
		return *this;
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt; Vector2&lt;T&gt;::operator*(const Vector2&lt;T&gt;&amp; right)
	{
		return Vector2&lt;T&gt;(x * right.x, y * right.y);
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;&amp; Vector2&lt;T&gt;::operator/=(const Vector2&lt;T&gt;&amp; right)
	{
		x /= right.x;
		y /= right.y;
		return *this;
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt; Vector2&lt;T&gt;::operator/(const Vector2&lt;T&gt;&amp; right)
	{
		return Vector2&lt;T&gt;(x / right.x, y / right.y);
	}

	template&lt;typename T&gt;
	inline bool Vector2&lt;T&gt;::operator==(const Vector2&lt;T&gt;&amp; right)
	{
		return x == right.x ? y == right.y : false;
	}

	template&lt;typename T&gt;
	inline bool Vector2&lt;T&gt;::operator!=(const Vector2&lt;T&gt;&amp; right)
	{
		return !(*this == right);
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;&amp; Vector2&lt;T&gt;::operator-=(const T&amp; right)
	{
		x -= right;
		y -= right;
		return *this;
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt; Vector2&lt;T&gt;::operator-(const T&amp; right)
	{
		return Vector2&lt;T&gt;(x - right, y - right);
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;&amp; Vector2&lt;T&gt;::operator+=(const T&amp; right)
	{
		x += right;
		y += right;
		return *this;
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt; Vector2&lt;T&gt;::operator+(const T&amp; right)
	{
		return Vector2&lt;T&gt;(x + right, y + right);
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;&amp; Vector2&lt;T&gt;::operator*=(const T&amp; right)
	{
		x *= right;
		y *= right;
		return *this;
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt; Vector2&lt;T&gt;::operator*(const T&amp; right)
	{
		return Vector2&lt;T&gt;(x * right, y * right);
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt;&amp; Vector2&lt;T&gt;::operator/=(const T&amp; right)
	{
		x /= right;
		y /= right;
		return *this;
	}

	template&lt;typename T&gt;
	inline Vector2&lt;T&gt; Vector2&lt;T&gt;::operator/(const T&amp; right)
	{
		return Vector2&lt;T&gt;(x / right, y / right);
	}
}
</code></pre>
<pre><code>typedef Private::Vector2&lt;int&gt; Vector2I;
</code></pre>
<p>Damit kann ich erstmal die Vektoren miteinander addieren, substrahieren, multiplizieren, etc...</p>
<p>indem ich folgendes schreibe:</p>
<pre><code>Vector2I v1(1, 1);
	Vector2I v2(2, 2);
	Vector2I v3(3, 3);

	v1 = -v2;
	v1 = v1 - v2;
	v1 = v2 - v3;
</code></pre>
<p>was mir aber noch fehlt bzw. wollte ich fragen ob es überhaupt möglich ist auch solche operationen durchzuführen/zu überladen:</p>
<pre><code>v1 - v2; // Hier wird dann v1 als Referenz geändert zurückgegeben
v1 + v2; // etc...
</code></pre>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2504452</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2504452</guid><dc:creator><![CDATA[Giggle]]></dc:creator><pubDate>Wed, 03 Aug 2016 02:06:01 GMT</pubDate></item><item><title><![CDATA[Reply to Überladen von Operatoren on Wed, 03 Aug 2016 05:14:50 GMT]]></title><description><![CDATA[<p>Ja, das geht. Du darfst deine Operatoren definieren wie du möchtest. Aber es ist stark davon abzuraten, die übliche Semantik zu verändern. Was du beschreibst ist doch, was man üblicherweise <code>+=</code> nennen würde, nicht <code>+</code> .</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2504455</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2504455</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Wed, 03 Aug 2016 05:14:50 GMT</pubDate></item><item><title><![CDATA[Reply to Überladen von Operatoren on Wed, 03 Aug 2016 09:57:51 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">return x == right.x ? y == right.y : false;
</code></pre>
<p>merkwürdige Art, &amp;&amp; zu schreiben. <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/2504472</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2504472</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 03 Aug 2016 09:57:51 GMT</pubDate></item></channel></rss>