<?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[virtual operator== problem]]></title><description><![CDATA[<p>hallo zusammen!<br />
ich habe ein problem mit virtueller vererbung.</p>
<pre><code>class Base {
public:
	Base(){}
	virtual bool operator==(const Base &amp;other){ 
		return baseInt == other.baseInt;
	}
private:
	int baseInt = 1;
};

class A : virtual public Base {
public:
	A(int _a) : aInt(_a){}
	bool operator==(const Base &amp;other) override {
		return Base::operator==(other) &amp;&amp;
		aInt == static_cast&lt;A*&gt;(*other);   // geht nicht
	}
private:
	int aInt = 2;
};

Base *a1 = new A(5);
Base *a2 = new A(6);
bool test = *a1 == *a2;
</code></pre>
<p>wie kann ich aInt vergleichen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/333826/virtual-operator-problem</link><generator>RSS for Node</generator><lastBuildDate>Sun, 26 Apr 2026 08:58:11 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/333826.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 03 Aug 2015 08:51:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to virtual operator== problem on Mon, 03 Aug 2015 08:52:31 GMT]]></title><description><![CDATA[<p>hallo zusammen!<br />
ich habe ein problem mit virtueller vererbung.</p>
<pre><code>class Base {
public:
	Base(){}
	virtual bool operator==(const Base &amp;other){ 
		return baseInt == other.baseInt;
	}
private:
	int baseInt = 1;
};

class A : virtual public Base {
public:
	A(int _a) : aInt(_a){}
	bool operator==(const Base &amp;other) override {
		return Base::operator==(other) &amp;&amp;
		aInt == static_cast&lt;A*&gt;(*other);   // geht nicht
	}
private:
	int aInt = 2;
};

Base *a1 = new A(5);
Base *a2 = new A(6);
bool test = *a1 == *a2;
</code></pre>
<p>wie kann ich aInt vergleichen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2462194</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2462194</guid><dc:creator><![CDATA[mael15]]></dc:creator><pubDate>Mon, 03 Aug 2015 08:52:31 GMT</pubDate></item><item><title><![CDATA[Reply to virtual operator== problem on Mon, 03 Aug 2015 08:59:16 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">class Base {
public:
  Base(){}
  bool operator==(const Base &amp;other){
    return compare(other) &amp;&amp; other.compare(*this);
  }
protected:
  virtual bool compare(const Base &amp;other) const {
    return baseInt == other.baseInt;
  }
private:
  int baseInt = 1;
};

class A : virtual public Base {
public:
  A(int _a) : aInt(_a){}
protected:
  bool compare(const Base &amp;other) const override {
    auto *o = dynamic_cast&lt;const A*&gt;(&amp;other);
    return o &amp;&amp; Base::compare(other) &amp;&amp; aInt == o-&gt;aInt;
  }
private:
  int aInt = 2;
};

auto a1 = std::make_unique&lt;A&gt;(5);
auto a2 = std::make_unique&lt;A&gt;(6);
bool test = *a1 == *a2;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2462196</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2462196</guid><dc:creator><![CDATA[vive]]></dc:creator><pubDate>Mon, 03 Aug 2015 08:59:16 GMT</pubDate></item><item><title><![CDATA[Reply to virtual operator== problem on Mon, 03 Aug 2015 09:00:51 GMT]]></title><description><![CDATA[<p>Korrektur: Da muss noch ein</p>
<pre><code class="language-cpp">virtual ~Base(){}
</code></pre>
<p>rein und unten sollte es heissen</p>
<pre><code class="language-cpp">std::unique_ptr&lt;Base&gt; a1 = std::make_unique&lt;A&gt;(5);
std::unique_ptr&lt;Base&gt; a2 = std::make_unique&lt;A&gt;(6);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2462197</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2462197</guid><dc:creator><![CDATA[vive]]></dc:creator><pubDate>Mon, 03 Aug 2015 09:00:51 GMT</pubDate></item><item><title><![CDATA[Reply to virtual operator== problem on Mon, 03 Aug 2015 09:01:48 GMT]]></title><description><![CDATA[<p>wow, das war schnell, danke!<br />
das wirkt recht kompliziert, ginge es auch einfacher?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2462198</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2462198</guid><dc:creator><![CDATA[mael15]]></dc:creator><pubDate>Mon, 03 Aug 2015 09:01:48 GMT</pubDate></item><item><title><![CDATA[Reply to virtual operator== problem on Mon, 03 Aug 2015 09:08:41 GMT]]></title><description><![CDATA[<p>scheint, als ob</p>
<pre><code>auto *o = dynamic_cast&lt;const A*&gt;(&amp;other);
</code></pre>
<p>reichen würde und der Base::compare nicht nötig wäre.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2462200</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2462200</guid><dc:creator><![CDATA[mael15]]></dc:creator><pubDate>Mon, 03 Aug 2015 09:08:41 GMT</pubDate></item><item><title><![CDATA[Reply to virtual operator== problem on Mon, 03 Aug 2015 10:26:57 GMT]]></title><description><![CDATA[<p>Operatorüberladung + Polymorphie? Für gewöhnlich keine gute Idee.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2462206</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2462206</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 03 Aug 2015 10:26:57 GMT</pubDate></item><item><title><![CDATA[Reply to virtual operator== problem on Mon, 03 Aug 2015 18:05:04 GMT]]></title><description><![CDATA[<p>Hä, Operatoren können echt virtuell sein? Hab ich noch nie gesehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2462274</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2462274</guid><dc:creator><![CDATA[Mechanics]]></dc:creator><pubDate>Mon, 03 Aug 2015 18:05:04 GMT</pubDate></item><item><title><![CDATA[Reply to virtual operator== problem on Mon, 03 Aug 2015 18:47:24 GMT]]></title><description><![CDATA[<p>Mechanics schrieb:</p>
<blockquote>
<p>Hä, Operatoren können echt virtuell sein? Hab ich noch nie gesehen.</p>
</blockquote>
<p>Natürlich. Sind lediglich Funktionen mit sonderlichen Namen und Aufruf via Infix-Notation. Dieser Wolf-Spinner hat sogar ein Kapitel (4.8.5) über polymorphe Zuweisungsoperatoren in seinem berüchtigten Erguss erstellt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2462290</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2462290</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Mon, 03 Aug 2015 18:47:24 GMT</pubDate></item><item><title><![CDATA[Reply to virtual operator== problem on Mon, 03 Aug 2015 18:59:10 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<p>Natürlich.</p>
</blockquote>
<p>Ist mir echt noch nie untergekommen. Wenn man nach polymorphic operators sucht, findet man eher sowas:</p>
<p><a href="http://www.drdobbs.com/cpp/simulating-polymorphic-operators-in-c/200001978" rel="nofollow">http://www.drdobbs.com/cpp/simulating-polymorphic-operators-in-c/200001978</a></p>
<p>Da gehts zwar (zumindest auf der ersten Seite, hab nicht alles durchgeschaut) um Operatoren, die als freie Funktionen implementiert sind, aber das ganze vermittelt doch den Eindruck, als ob das nicht direkt mit virtual gehen würde.</p>
<p>Ok, hat jemand irgendwelche Erfahrungen damit, was spricht konkret dagegen? Ein Vergleichsoperator ist wahrscheinlich nicht unproblematisch, man bräuchte fast double dispatch, sonst ergibt a == b evtl. was anderes als b == a. Allerdings hat man dasselbe Problem, wenn man eine equals Funktion einbaut, das machts ja nicht besser.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2462294</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2462294</guid><dc:creator><![CDATA[Mechanics]]></dc:creator><pubDate>Mon, 03 Aug 2015 18:59:10 GMT</pubDate></item><item><title><![CDATA[Reply to virtual operator== problem on Tue, 04 Aug 2015 05:57:59 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<p>Mechanics schrieb:</p>
<blockquote>
<p>Hä, Operatoren können echt virtuell sein? Hab ich noch nie gesehen.</p>
</blockquote>
<p>Natürlich. Sind lediglich Funktionen mit sonderlichen Namen und Aufruf via Infix-Notation. Dieser Wolf-Spinner hat sogar ein Kapitel (4.8.5) über polymorphe Zuweisungsoperatoren in seinem berüchtigten Erguss erstellt.</p>
</blockquote>
<p>Ach, Du hast den Wolf gelesen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /> .</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2462339</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2462339</guid><dc:creator><![CDATA[tntnet]]></dc:creator><pubDate>Tue, 04 Aug 2015 05:57:59 GMT</pubDate></item><item><title><![CDATA[Reply to virtual operator== problem on Tue, 04 Aug 2015 06:55:45 GMT]]></title><description><![CDATA[<p>Mechanics schrieb:</p>
<blockquote>
<p>was spricht konkret dagegen?</p>
</blockquote>
<p>dagegen spricht, dass man zum überladen dieselbe signatur benutzen und dann dynamisch casten muss. is nicht so elegant, funktioniert aber:</p>
<pre><code>class Base {
	public:
		Base(){}
		virtual bool operator==(const Base &amp;other) { 
			return baseInt == other.baseInt;
		}
		Base(const Base &amp;other){
			baseInt = other.baseInt;
		}
	private:
		int baseInt = 1;
	};

	class A : virtual public Base {
	public:
		A(int _a) : aInt(_a){}
		virtual bool operator==(const Base &amp;other) override {
			auto *o = dynamic_cast&lt;const A*&gt;(&amp;other);
			return Base::operator==(other) &amp;&amp; o &amp;&amp; aInt == o-&gt;aInt;
		}
		A(const A &amp;other) : Base(other){
			aInt = other.aInt;
		}
	private:
		int aInt = 2;
	};

	class A2 : virtual public Base {
	public:
		A2(int _a) : a2Int(_a){}
		virtual bool operator==(const Base &amp;other) override {
			auto *o = dynamic_cast&lt;const A2*&gt;(&amp;other);
			return Base::operator==(other) &amp;&amp; o &amp;&amp; a2Int == o-&gt;a2Int;
		}
		A2(const A2 &amp;other) : Base(other){
			a2Int = other.a2Int;
		}
	private:
		int a2Int = 2;
	};

	class B : public A, public A2 {
	public:
		B(int _b) : bInt(_b), A(_b * 2), A2(_b*3){}
		bool operator==(const Base &amp;other){
			auto b = dynamic_cast&lt;const B*&gt;(&amp;other);
			return A::operator==(other) &amp;&amp; A2::operator==(other) &amp;&amp; b &amp;&amp; b-&gt;bInt == bInt;
		}
		B(const B &amp;other) : Base(other), A(other), A2(other){
			bInt = other.bInt;
		}
	private:
		int bInt = 3;
	};
</code></pre>
<p>und in diesem fall wird Base::operator== doppelt aufgerufen.<br />
und beim hinzufügen weiterer klassen darf man das nicht vergessen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2462345</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2462345</guid><dc:creator><![CDATA[mael15]]></dc:creator><pubDate>Tue, 04 Aug 2015 06:55:45 GMT</pubDate></item><item><title><![CDATA[Reply to virtual operator== problem on Tue, 04 Aug 2015 09:22:14 GMT]]></title><description><![CDATA[<p>mael15 schrieb:</p>
<blockquote>
<p>...</p>
</blockquote>
<p>Wichtig ist, dass der Vergleichsoperator symmetrisch ist (a==b &lt;=&gt; b==a). Die beste Lösung dazu ist, zwei Vergleiche durchzuführen, wie ich das gezeigt habe:</p>
<pre><code class="language-cpp">return compare(other) &amp;&amp; other.compare(*this);
</code></pre>
<p>Bei dir ist das soweit ich sehe nicht erfüllt (Base()==A()).</p>
<p>Mit virtuellen operator== hätte man da exponentiell viele Vergleiche.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2462365</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2462365</guid><dc:creator><![CDATA[vive]]></dc:creator><pubDate>Tue, 04 Aug 2015 09:22:14 GMT</pubDate></item><item><title><![CDATA[Reply to virtual operator== problem on Tue, 04 Aug 2015 19:04:02 GMT]]></title><description><![CDATA[<p>mael15 schrieb:</p>
<blockquote>
<p>dagegen spricht, dass man zum überladen dieselbe signatur benutzen und dann dynamisch casten muss.</p>
</blockquote>
<p>Das ist ja im Endeffekt das, was ich auch gleich angemerkt habe, man kann dann leicht einen Vergleichsoperator bauen, der nicht symmetrisch ist. Das Problem ist dabei aber nicht der Operator, sondern der Vergleich.<br />
Dasselbe Problem hätte man doch, wenn man eine Funktion virtual bool equals(const Base&amp; other) hinzufügen würde. Und das ist eine Design Entscheidung, ob man sowas überhaupt haben will und wie man damit umgeht.<br />
Ich meine, wir hätten auch sowas ähnliches bei uns. Wir haben irgendwelche &quot;Container&quot; Klassen, über die man irgendwelche dynamischen Operationen abfragen kann. Und ich glaub, die haben eben auch so eine virtuelle equals Funktion. Die Basisversion ruft irgendwelche allgemeinen Infos ab und vergleicht die. Und es gibt Ableitungen, die mehr vergleichen können. z.B., wenn das Teil mit Informationen aus einem PDM angereichert wurde, auch die PDM ID vergleichen. Das dürfte allerdings nur Sinn machen, wenn man da einen dynamic_cast, sonst wär der Vergleich eben nicht symmetrisch. Ich weiß nicht, ob das bei uns so gemacht wurde oder obs da wo es verwendet wird egal ist. Jedenfalls ist es an der Stelle eben völlig egal, obs eine Operator oder eine Funktion ist und dann kann man genauso einen Operator nehmen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2462494</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2462494</guid><dc:creator><![CDATA[Mechanics]]></dc:creator><pubDate>Tue, 04 Aug 2015 19:04:02 GMT</pubDate></item><item><title><![CDATA[Reply to virtual operator== problem on Tue, 04 Aug 2015 20:49:40 GMT]]></title><description><![CDATA[<p>vive schrieb:</p>
<blockquote>
<p>mael15 schrieb:</p>
<blockquote>
<p>...</p>
</blockquote>
<p>Wichtig ist, dass der Vergleichsoperator symmetrisch ist (a==b &lt;=&gt; b==a). Die beste Lösung dazu ist, zwei Vergleiche durchzuführen, wie ich das gezeigt habe:</p>
<pre><code class="language-cpp">return compare(other) &amp;&amp; other.compare(*this);
</code></pre>
</blockquote>
<p>Das war auch meine 1. Idee. Ist aber potentiell teuer.<br />
Wie wäre es mit</p>
<pre><code class="language-cpp">return typeid(*this) == typeid(other) &amp;&amp; compare(other);
</code></pre>
<p>?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2462525</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2462525</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 04 Aug 2015 20:49:40 GMT</pubDate></item><item><title><![CDATA[Reply to virtual operator== problem on Tue, 04 Aug 2015 21:02:12 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p>Wie wäre es mit</p>
<pre><code class="language-cpp">return typeid(*this) == typeid(other) &amp;&amp; compare(other);
</code></pre>
<p>?</p>
</blockquote>
<p>Kannst du machen, schränkt dich aber u.U. ein (ein SparseVector kann so nicht mehr gleich sein wie ein DenseVector, oder vielleicht verhält sich Klasse A von mael15 gleich wie Base wenn aInt==0).</p>
<p>Können diese Fälle ausgeschlossen werden, ist deine Version natürlich schneller.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2462527</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2462527</guid><dc:creator><![CDATA[vive]]></dc:creator><pubDate>Tue, 04 Aug 2015 21:02:12 GMT</pubDate></item></channel></rss>