<?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[Operator Funktionen]]></title><description><![CDATA[<p>Hi,<br />
ich hab mir ein smples Programm geschrieben:</p>
<pre><code class="language-cpp">*.h
#ifndef _operator_h_
#define _operator_h_
using namespace std;

class Rechteck
{
	int SeiteA;
	int SeiteB;
public:
	Rechteck(int sA,int sB);
	bool operator==(Rechteck y);
	int operator+(Rechteck z);
	friend bool operator==(Rechteck &amp;x,Rechteck &amp;y);
	void print();
	friend int operator+(Rechteck &amp;x,Rechteck &amp;y);

};

#endif
</code></pre>
<pre><code class="language-cpp">*.cpp
#include &lt;iostream&gt;
#include &quot;op_header.h&quot;
using namespace std;

Rechteck::Rechteck(int sA,int sB)
{ 
	SeiteA=sA;
	SeiteB=sB;
}
inline void Rechteck::print()
{
	cout &lt;&lt; &quot;\nSeite A= &quot; &lt;&lt; SeiteA &lt;&lt; &quot;\n&quot;;
	cout &lt;&lt; &quot;\nSeite B= &quot; &lt;&lt; SeiteB &lt;&lt; &quot;\n&quot;;
	cin.ignore();
}

bool Rechteck::operator ==(Rechteck y)
{
	if(this-&gt;SeiteA==y.SeiteB &amp;&amp; this-&gt;SeiteB == y.SeiteA)
		return true;
	else
		return false;
}

bool operator==(Rechteck &amp;a, Rechteck &amp;b)
{
	return( a.SeiteA==b.SeiteB &amp;&amp; a.SeiteB==b.SeiteA);
}

int operator+(Rechteck &amp;a, Rechteck &amp;b)
{
	return a.SeiteA+a.SeiteB+b.SeiteA+b.SeiteB;
}

int Rechteck::operator +(Rechteck z)
{
	return this-&gt;SeiteA+z.SeiteB+this-&gt;SeiteB+z.SeiteA;
}

int main(void)
{
	Rechteck z(2,2);
	Rechteck y(2,2);
	if (z==y) // &lt;-******_____------**++++++++++++****-----********HIER
		cout &lt;&lt;&quot;jouu&quot;;

	z.print();
	cout &lt;&lt;&quot;\n look == : &quot; &lt;&lt; y.operator==(z);
	cout &lt;&lt;&quot;\n look == 2nd: &quot; &lt;&lt; operator==(z,y);
	cout &lt;&lt;&quot;\n look + : &quot; &lt;&lt; y.operator +(z);
	cout &lt;&lt;&quot;\n look + 2nd : &quot; &lt;&lt; operator +(z,y);

	cin.ignore();
}
</code></pre>
<p>Es geht um die markierte Zeile. kann ich durch so etwas nicht die operatorFkt's angeben oder hab ich da einfach nur en Fehler gemacht?? Wenn nein weshalb benutzt man denn die operatorenFunktionen?</p>
<p>mfG<br />
Phrua@</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/134430/operator-funktionen</link><generator>RSS for Node</generator><lastBuildDate>Fri, 28 Aug 2026 12:27:49 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/134430.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 26 Jan 2006 00:20:16 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Operator Funktionen on Thu, 26 Jan 2006 00:20:16 GMT]]></title><description><![CDATA[<p>Hi,<br />
ich hab mir ein smples Programm geschrieben:</p>
<pre><code class="language-cpp">*.h
#ifndef _operator_h_
#define _operator_h_
using namespace std;

class Rechteck
{
	int SeiteA;
	int SeiteB;
public:
	Rechteck(int sA,int sB);
	bool operator==(Rechteck y);
	int operator+(Rechteck z);
	friend bool operator==(Rechteck &amp;x,Rechteck &amp;y);
	void print();
	friend int operator+(Rechteck &amp;x,Rechteck &amp;y);

};

#endif
</code></pre>
<pre><code class="language-cpp">*.cpp
#include &lt;iostream&gt;
#include &quot;op_header.h&quot;
using namespace std;

Rechteck::Rechteck(int sA,int sB)
{ 
	SeiteA=sA;
	SeiteB=sB;
}
inline void Rechteck::print()
{
	cout &lt;&lt; &quot;\nSeite A= &quot; &lt;&lt; SeiteA &lt;&lt; &quot;\n&quot;;
	cout &lt;&lt; &quot;\nSeite B= &quot; &lt;&lt; SeiteB &lt;&lt; &quot;\n&quot;;
	cin.ignore();
}

bool Rechteck::operator ==(Rechteck y)
{
	if(this-&gt;SeiteA==y.SeiteB &amp;&amp; this-&gt;SeiteB == y.SeiteA)
		return true;
	else
		return false;
}

bool operator==(Rechteck &amp;a, Rechteck &amp;b)
{
	return( a.SeiteA==b.SeiteB &amp;&amp; a.SeiteB==b.SeiteA);
}

int operator+(Rechteck &amp;a, Rechteck &amp;b)
{
	return a.SeiteA+a.SeiteB+b.SeiteA+b.SeiteB;
}

int Rechteck::operator +(Rechteck z)
{
	return this-&gt;SeiteA+z.SeiteB+this-&gt;SeiteB+z.SeiteA;
}

int main(void)
{
	Rechteck z(2,2);
	Rechteck y(2,2);
	if (z==y) // &lt;-******_____------**++++++++++++****-----********HIER
		cout &lt;&lt;&quot;jouu&quot;;

	z.print();
	cout &lt;&lt;&quot;\n look == : &quot; &lt;&lt; y.operator==(z);
	cout &lt;&lt;&quot;\n look == 2nd: &quot; &lt;&lt; operator==(z,y);
	cout &lt;&lt;&quot;\n look + : &quot; &lt;&lt; y.operator +(z);
	cout &lt;&lt;&quot;\n look + 2nd : &quot; &lt;&lt; operator +(z,y);

	cin.ignore();
}
</code></pre>
<p>Es geht um die markierte Zeile. kann ich durch so etwas nicht die operatorFkt's angeben oder hab ich da einfach nur en Fehler gemacht?? Wenn nein weshalb benutzt man denn die operatorenFunktionen?</p>
<p>mfG<br />
Phrua@</p>
]]></description><link>https://www.c-plusplus.net/forum/post/976470</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/976470</guid><dc:creator><![CDATA[Phruaed]]></dc:creator><pubDate>Thu, 26 Jan 2006 00:20:16 GMT</pubDate></item><item><title><![CDATA[Reply to Operator Funktionen on Thu, 26 Jan 2006 01:09:20 GMT]]></title><description><![CDATA[<p>Hallo,<br />
der Compiler hat ein Problem mit diesen beiden Sachen:</p>
<pre><code class="language-cpp">bool Rechteck::operator ==(Rechteck y)
{
    if(this-&gt;SeiteA==y.SeiteB &amp;&amp; this-&gt;SeiteB == y.SeiteA)
        return true;
    else
        return false;
}
</code></pre>
<p>und</p>
<pre><code class="language-cpp">bool operator==(Rechteck &amp;a, Rechteck &amp;b)
{
    return( a.SeiteA==b.SeiteB &amp;&amp; a.SeiteB==b.SeiteA);
}
</code></pre>
<p>Der Compiler weiß dann wenn du z==y schreibst nicht mehr, welche von den beiden Funktionen er aufrufen soll. Wenn du bloß eine der beiden Funktionen im Quellcode lässt kannst du den Test auf Gleichheit so anstellen, wie du das auch bei normalen Variablen kennst: also z==y. Das funktioniert auch beim +-Operator, aber dann musst du auch ein Klassenobjekt zurückgeben. Wenn du zum Beispiel so einen Text in deinen Code schreiben würdest:</p>
<pre><code class="language-cpp">z+y;
</code></pre>
<p>Und du würdest auch eine von den beiden Funktionen die den +-Operator überladen rausnehmen, würde der Compiler nicht meckern, aber du würdest ein völlig sinnloses Ergebnis rauskommen, nämlich ein int-Wert. Das ist in etwa so wie wenn du eine Birne plus 2 Birnen rechnest und bekommst als Ergebnis einen Apfel. <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="😉"
    /><br />
Deshalb überlädt man ja auch die Operatoren, damit man mit Klassen fast so rechnen kann wie mit normalen Variablen.<br />
Deshalb ist das auch relativ sinnlos:</p>
<pre><code class="language-cpp">y.operator==(z);
operator==(z,y);
y.operator +(z);
operator +(z,y);
</code></pre>
<p>Da hätte man genau so gut normale Funktionen schreiben können <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>
<p>MfG</p>
]]></description><link>https://www.c-plusplus.net/forum/post/976476</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/976476</guid><dc:creator><![CDATA[Jaschi]]></dc:creator><pubDate>Thu, 26 Jan 2006 01:09:20 GMT</pubDate></item></channel></rss>