<?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[Verträglichkeit zweier Typen]]></title><description><![CDATA[<p>Hallo NG,</p>
<p>ich habe eine Schnittstelle, bei der eine ID mittels unsigned int übermittelt wird. Auf meiner Seite der Schnittstelle habe ich (im nachhinein)<br />
eine Klasse NodeID_t gebaut, die diese ID kapselt. Wie weit kann ich die Typen kompatibel machen?</p>
<p>Habe schon folgede Dinge überschrieben:</p>
<pre><code>class NodeID_t
{
public:
	NodeID_t();
	NodeID_t(unsigned int id);
	NodeID_t(const NodeID_t&amp; rs);
	bool operator==(const NodeID_t&amp; rs) const;
	bool operator==(const unsigned int rs) const;
	bool operator!=(const NodeID_t&amp; rs) const;
	bool operator!=(const unsigned int rs) const;
	NodeID_t&amp; operator=(const NodeID_t&amp; rs);
	NodeID_t&amp; operator=(unsigned int rs);
	operator unsigned int () const;

	friend bool operator==(const unsigned int&amp; ls, const NodeID_t&amp; rs);
	friend bool operator!=(const unsigned int&amp; ls, const NodeID_t&amp; rs);
</code></pre>
<p>Trotzdem bekomme ich noch Fehlermeldungen, wenn eine Methode eine NodeID_t verlangt und ich ein unsigned int übergebe.<br />
Kann man das auch noch umgehen?</p>
<p>Grüße<br />
Timo</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/233659/verträglichkeit-zweier-typen</link><generator>RSS for Node</generator><lastBuildDate>Thu, 24 Sep 2026 15:11:01 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/233659.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 07 Feb 2009 09:27:08 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Verträglichkeit zweier Typen on Sat, 07 Feb 2009 09:27:08 GMT]]></title><description><![CDATA[<p>Hallo NG,</p>
<p>ich habe eine Schnittstelle, bei der eine ID mittels unsigned int übermittelt wird. Auf meiner Seite der Schnittstelle habe ich (im nachhinein)<br />
eine Klasse NodeID_t gebaut, die diese ID kapselt. Wie weit kann ich die Typen kompatibel machen?</p>
<p>Habe schon folgede Dinge überschrieben:</p>
<pre><code>class NodeID_t
{
public:
	NodeID_t();
	NodeID_t(unsigned int id);
	NodeID_t(const NodeID_t&amp; rs);
	bool operator==(const NodeID_t&amp; rs) const;
	bool operator==(const unsigned int rs) const;
	bool operator!=(const NodeID_t&amp; rs) const;
	bool operator!=(const unsigned int rs) const;
	NodeID_t&amp; operator=(const NodeID_t&amp; rs);
	NodeID_t&amp; operator=(unsigned int rs);
	operator unsigned int () const;

	friend bool operator==(const unsigned int&amp; ls, const NodeID_t&amp; rs);
	friend bool operator!=(const unsigned int&amp; ls, const NodeID_t&amp; rs);
</code></pre>
<p>Trotzdem bekomme ich noch Fehlermeldungen, wenn eine Methode eine NodeID_t verlangt und ich ein unsigned int übergebe.<br />
Kann man das auch noch umgehen?</p>
<p>Grüße<br />
Timo</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1659420</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1659420</guid><dc:creator><![CDATA[horscht2999]]></dc:creator><pubDate>Sat, 07 Feb 2009 09:27:08 GMT</pubDate></item><item><title><![CDATA[Reply to Verträglichkeit zweier Typen on Sat, 07 Feb 2009 10:10:23 GMT]]></title><description><![CDATA[<p>Ist der Parameter eine const-Referenz oder ein call by value?<br />
An sich würde ich aber eher zu einem expliziten Cast raten. Das ein int automatisch zu deiner Klasse werden kann, kann dir auch auf die Füße fallen. Normalerweise macht man bei sowas den Konstruktor &quot;explicit&quot;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1659431</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1659431</guid><dc:creator><![CDATA[Pellaeon]]></dc:creator><pubDate>Sat, 07 Feb 2009 10:10:23 GMT</pubDate></item><item><title><![CDATA[Reply to Verträglichkeit zweier Typen on Sat, 07 Feb 2009 10:56:46 GMT]]></title><description><![CDATA[<p>Pellaeon schrieb:</p>
<blockquote>
<p>Ist der Parameter eine const-Referenz oder ein call by value?</p>
</blockquote>
<p>Eine const Referenz.</p>
<p>Pellaeon schrieb:</p>
<blockquote>
<p>An sich würde ich aber eher zu einem expliziten Cast raten. Das ein int automatisch zu deiner Klasse werden kann, kann dir auch auf die Füße fallen.</p>
</blockquote>
<p>Die int Sachen habe ich auch implementiert, nur nicht mit angegeben in meinem vorrangegangenen Post. Wie gesagt es war alles schon mit unsigned int implementiert. Ein cast würde so um die 500 Änderungen nach sich ziehen, da bin ich zu faul. Die Umwandlung in eine eigne Klasse hat auch mehr ideelle Gründe:-)</p>
<p>Pellaeon schrieb:</p>
<blockquote>
<p>Normalerweise macht man bei sowas den Konstruktor &quot;explicit&quot;</p>
</blockquote>
<p>Das hat scheinbar nicht gewirkt:-(</p>
<p>Ciao<br />
Timo</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1659448</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1659448</guid><dc:creator><![CDATA[horscht2999]]></dc:creator><pubDate>Sat, 07 Feb 2009 10:56:46 GMT</pubDate></item><item><title><![CDATA[Reply to Verträglichkeit zweier Typen on Sat, 07 Feb 2009 11:20:58 GMT]]></title><description><![CDATA[<p>Okay habs jetzt hinbekommen.</p>
<p>Habe folgende Methoden überschrieben:</p>
<pre><code>class NodeID_t
{
public:
	NodeID_t();
	NodeID_t(const NodeID_t&amp; rs);
	explicit NodeID_t(const unsigned int&amp; id);
	NodeID_t(unsigned int rs);
	explicit NodeID_t(const int&amp; rs);
	NodeID_t(int rs);
	bool operator==(const NodeID_t&amp; rs) const;
	bool operator==(const unsigned int rs) const;
	bool operator==(const int rs) const;
	friend bool operator==(const unsigned int&amp; ls, const NodeID_t&amp; rs);
	friend bool operator==(const int&amp; ls, const NodeID_t&amp; rs);
	bool operator!=(const NodeID_t&amp; rs) const;
	bool operator!=(const unsigned int rs) const;
	bool operator!=(const int rs) const;
	friend bool operator!=(const unsigned int&amp; ls, const NodeID_t&amp; rs);
	friend bool operator!=(const int&amp; ls, const NodeID_t&amp; rs);
	NodeID_t&amp; operator=(const NodeID_t&amp; rs);
	NodeID_t&amp; operator=(unsigned int rs);
	NodeID_t&amp; operator=(int rs);
	operator unsigned int () const;
	operator int () const;
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1659452</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1659452</guid><dc:creator><![CDATA[horscht2999]]></dc:creator><pubDate>Sat, 07 Feb 2009 11:20:58 GMT</pubDate></item><item><title><![CDATA[Reply to Verträglichkeit zweier Typen on Sun, 08 Feb 2009 10:19:14 GMT]]></title><description><![CDATA[<p>Wieso machst du eigentlich vom selben Operator ( <code>==</code> , <code>!=</code> ) gewisse Überladungen global und gewisse als Member? Ich würde die einheitlich global machen. Oder wenn du es schon so machst, kannst du das <code>friend</code> weglassen, indem du bei den globalen Operatoren auf die Member-Operatoren zugreifst.</p>
<p>Und das <code>const</code> bei Parameter von skalaren Typen (z.B. <code>const unsigned int</code> ) ist auch eher unüblich und nicht wirklich nötig, da das Argument sowieso kopiert wird. Was bezweckst du eigentlich mit Konstruktoren für <code>int</code> und <code>const int&amp;</code> ? Das führt zu Mehrdeutigkeiten beim Aufruf.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1659847</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1659847</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sun, 08 Feb 2009 10:19:14 GMT</pubDate></item><item><title><![CDATA[Reply to Verträglichkeit zweier Typen on Sun, 08 Feb 2009 17:14:06 GMT]]></title><description><![CDATA[<p>Ehrlich gesagt kann ich Dir darauf nicht antworten. Mangels ausreichender Kenntnisse habe ich einfach alles überschrieben, was mir einfiel, bis die Kompilerfehler verschwunden waren, ohne das ich casten musste. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":-("
      alt="😞"
    /></p>
<p>Ich oute mich hiermit als Anfänger!</p>
<p>Ciao<br />
Timo</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1660069</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1660069</guid><dc:creator><![CDATA[horscht2999]]></dc:creator><pubDate>Sun, 08 Feb 2009 17:14:06 GMT</pubDate></item><item><title><![CDATA[Reply to Verträglichkeit zweier Typen on Mon, 09 Feb 2009 14:39:43 GMT]]></title><description><![CDATA[<p>Mach das explicit wieder weg! Du *willst* ja den automatischen Cast. Das explicit macht genau das Gegenteil^^<br />
Und wie schon drüber erwähnt: &quot;const int&amp;&quot; macht auch nicht wirklich Sinn. EInfach nur &quot;int&quot; reicht (generell bei den Grundtypen, also auch bei float und unsigned usw.)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1660521</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1660521</guid><dc:creator><![CDATA[Pellaeon]]></dc:creator><pubDate>Mon, 09 Feb 2009 14:39:43 GMT</pubDate></item><item><title><![CDATA[Reply to Verträglichkeit zweier Typen on Mon, 09 Feb 2009 14:43:33 GMT]]></title><description><![CDATA[<p>Alles klar.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1660524</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1660524</guid><dc:creator><![CDATA[horscht2999]]></dc:creator><pubDate>Mon, 09 Feb 2009 14:43:33 GMT</pubDate></item><item><title><![CDATA[Reply to Verträglichkeit zweier Typen on Mon, 09 Feb 2009 17:21:26 GMT]]></title><description><![CDATA[<p>Pellaeon schrieb:</p>
<blockquote>
<p>Und wie schon drüber erwähnt: &quot;const int&amp;&quot; macht auch nicht wirklich Sinn. EInfach nur &quot;int&quot; reicht (generell bei den Grundtypen, also auch bei float und unsigned usw.)</p>
</blockquote>
<p>Es macht nicht nur keinen Sinn, sondern sollte auch wie erwähnt zu Mehrdeutigkeiten führen. Mich wundert, dass das scheinbar geklappt hat. Wurde der Konstruktor überhaupt jemals aufgerufen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1660625</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1660625</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Mon, 09 Feb 2009 17:21:26 GMT</pubDate></item><item><title><![CDATA[Reply to Verträglichkeit zweier Typen on Mon, 09 Feb 2009 17:25:57 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>Wurde der Konstruktor überhaupt jemals aufgerufen?</p>
</blockquote>
<p>Scheinbar nicht. Wie schon geschrieben, ich habe einfach alles überschrieben, was mir einfiel, bis alle Kompilerfehler weg waren. Vieles davon ist sicher überflüssig. Aber was, das wißt nur ihr.</p>
<p>Danke an alle, die mir helfen!</p>
<p>Ciao<br />
Timo</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1660626</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1660626</guid><dc:creator><![CDATA[horscht2999]]></dc:creator><pubDate>Mon, 09 Feb 2009 17:25:57 GMT</pubDate></item><item><title><![CDATA[Reply to Verträglichkeit zweier Typen on Mon, 09 Feb 2009 17:42:59 GMT]]></title><description><![CDATA[<p>Gut, dann nimm aber auch noch die mehrdeutigen Überladungen weg, wenn du nicht gleich wieder Compilerfehler haben willst. <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>
]]></description><link>https://www.c-plusplus.net/forum/post/1660646</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1660646</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Mon, 09 Feb 2009 17:42:59 GMT</pubDate></item><item><title><![CDATA[Reply to Verträglichkeit zweier Typen on Mon, 09 Feb 2009 18:26:26 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>Wieso machst du eigentlich vom selben Operator (==, !=) gewisse Überladungen global und gewisse als Member? Ich würde die einheitlich global machen. Oder wenn du es schon so machst, kannst du das friend weglassen, indem du bei den globalen Operatoren auf die Member-Operatoren zugreifst.</p>
</blockquote>
<p>Jetzt hab ich es gemerkt <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="🙂"
    /> Das sollten die Umkehrfunktionen sein, also wenn NodeID_t nicht rechts steht beim Vergleich. Hab jetzt aber gesehen, dass ich sie falschrum definiert habe.<br />
Das mit dem nicht friend, hab ich übernommen.</p>
<p>Ciao<br />
Timo</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1660690</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1660690</guid><dc:creator><![CDATA[horscht2999]]></dc:creator><pubDate>Mon, 09 Feb 2009 18:26:26 GMT</pubDate></item></channel></rss>