<?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[Impliziter Cast von char* nach bool]]></title><description><![CDATA[<p>Guten Tag liebe Gemeinde.</p>
<p>Hier mal ein Problem, was ich mir absolut nicht herleiten kann. Es wird ein char-Array(char*) implizit nach bool gecastet. Ich könnte mir vorstellen, dass die Adresse als long int gesehen wird. Und damit dann zu int und dann schliesslich zu bool. Aber seht selbst:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

struct MyClass {
	MyClass( bool x ) {
		std::cout &lt;&lt; &quot;Bool-Konstruktor mit x=&quot; &lt;&lt; x &lt;&lt; std::endl;
	}
};

int main( ) {
	MyClass mc( &quot;input&quot; );
	return 0;
}
</code></pre>
<p>Konsole:</p>
<pre><code>&gt; g++ -Wall -pedantic boolString.cpp -o boolString
&gt; ./boolString
Bool-Konstruktor mit x=1
&gt;
</code></pre>
<p>Die Frage ist hier, wieso der Kompiler das macht, und wie man das verhindert?<br />
Ich richtigen Programm(woraus das Problem ist), habe ich den Operator &lt;&lt; einer Klasse X überladen. Beispielsweise für die Klassen Date und String:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

struct String {
	String( const char *str = 0 ) { }
	String( const String&amp; str ) { }
};

struct Date {
	Date( const bool readCurrent = false ) { }
};

struct X {
	X&amp; operator&lt;&lt;( const String &amp;str ) {
		std::cout &lt;&lt; &quot;operator&lt;&lt;( string )&quot; &lt;&lt; std::endl;
		return *this;
	}
	X&amp; operator&lt;&lt;( const Date &amp;dat ) {
		std::cout &lt;&lt; &quot;operator&lt;&lt;( date )&quot; &lt;&lt; std::endl;
		return *this;
	}
	X&amp; operator&lt;&lt;( const int &amp;i ) {
		std::cout &lt;&lt; &quot;operator&lt;&lt;( date )&quot; &lt;&lt; std::endl;
		return *this;
	}
};

int main( ) {
	X x;
	Date dat( true );
	x &lt;&lt; 42;
	x &lt;&lt; &quot;foo&quot;; // ambiguous overload
	x &lt;&lt; dat;

	return 0;
}
</code></pre>
<p>Konsole:</p>
<pre><code>&gt; g++ -Wall -pedantic boolString.cpp -o boolString
boolString.cpp: In function ‘int main()’:
boolString.cpp:32:7: error: ambiguous overload for ‘operator&lt;&lt;’ in ‘x &lt;&lt; &quot;foo&quot;’
boolString.cpp:32:7: note: candidates are:
boolString.cpp:13:5: note: X&amp; X::operator&lt;&lt;(const String&amp;)
boolString.cpp:17:5: note: X&amp; X::operator&lt;&lt;(const Date&amp;)
&gt;
</code></pre>
<p>Wie kann man das Problem beheben im genannten Beispiel?</p>
<p>MfG. Christoph</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/304176/impliziter-cast-von-char-nach-bool</link><generator>RSS for Node</generator><lastBuildDate>Mon, 10 Aug 2026 00:49:57 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/304176.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 31 May 2012 11:33:37 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Impliziter Cast von char* nach bool on Thu, 31 May 2012 11:33:37 GMT]]></title><description><![CDATA[<p>Guten Tag liebe Gemeinde.</p>
<p>Hier mal ein Problem, was ich mir absolut nicht herleiten kann. Es wird ein char-Array(char*) implizit nach bool gecastet. Ich könnte mir vorstellen, dass die Adresse als long int gesehen wird. Und damit dann zu int und dann schliesslich zu bool. Aber seht selbst:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

struct MyClass {
	MyClass( bool x ) {
		std::cout &lt;&lt; &quot;Bool-Konstruktor mit x=&quot; &lt;&lt; x &lt;&lt; std::endl;
	}
};

int main( ) {
	MyClass mc( &quot;input&quot; );
	return 0;
}
</code></pre>
<p>Konsole:</p>
<pre><code>&gt; g++ -Wall -pedantic boolString.cpp -o boolString
&gt; ./boolString
Bool-Konstruktor mit x=1
&gt;
</code></pre>
<p>Die Frage ist hier, wieso der Kompiler das macht, und wie man das verhindert?<br />
Ich richtigen Programm(woraus das Problem ist), habe ich den Operator &lt;&lt; einer Klasse X überladen. Beispielsweise für die Klassen Date und String:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

struct String {
	String( const char *str = 0 ) { }
	String( const String&amp; str ) { }
};

struct Date {
	Date( const bool readCurrent = false ) { }
};

struct X {
	X&amp; operator&lt;&lt;( const String &amp;str ) {
		std::cout &lt;&lt; &quot;operator&lt;&lt;( string )&quot; &lt;&lt; std::endl;
		return *this;
	}
	X&amp; operator&lt;&lt;( const Date &amp;dat ) {
		std::cout &lt;&lt; &quot;operator&lt;&lt;( date )&quot; &lt;&lt; std::endl;
		return *this;
	}
	X&amp; operator&lt;&lt;( const int &amp;i ) {
		std::cout &lt;&lt; &quot;operator&lt;&lt;( date )&quot; &lt;&lt; std::endl;
		return *this;
	}
};

int main( ) {
	X x;
	Date dat( true );
	x &lt;&lt; 42;
	x &lt;&lt; &quot;foo&quot;; // ambiguous overload
	x &lt;&lt; dat;

	return 0;
}
</code></pre>
<p>Konsole:</p>
<pre><code>&gt; g++ -Wall -pedantic boolString.cpp -o boolString
boolString.cpp: In function ‘int main()’:
boolString.cpp:32:7: error: ambiguous overload for ‘operator&lt;&lt;’ in ‘x &lt;&lt; &quot;foo&quot;’
boolString.cpp:32:7: note: candidates are:
boolString.cpp:13:5: note: X&amp; X::operator&lt;&lt;(const String&amp;)
boolString.cpp:17:5: note: X&amp; X::operator&lt;&lt;(const Date&amp;)
&gt;
</code></pre>
<p>Wie kann man das Problem beheben im genannten Beispiel?</p>
<p>MfG. Christoph</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217684</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217684</guid><dc:creator><![CDATA[ChristophLu]]></dc:creator><pubDate>Thu, 31 May 2012 11:33:37 GMT</pubDate></item><item><title><![CDATA[Reply to Impliziter Cast von char* nach bool on Thu, 31 May 2012 11:36:45 GMT]]></title><description><![CDATA[<p>Du könntest den Konstruktor von Date <code>explicit</code> machen. Dann gibt es keine implizite Umwandlung von <code>bool</code> nach Date mehr, man muss dann immer bspw. <code>Date(true)</code> schreiben (was IMHO auch unabhängig von dem Überladungsproblem sinnvoll ist in diesem Fall.)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217690</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217690</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Thu, 31 May 2012 11:36:45 GMT</pubDate></item><item><title><![CDATA[Reply to Impliziter Cast von char* nach bool on Thu, 31 May 2012 13:22:26 GMT]]></title><description><![CDATA[<p>Pointer sind implizit nach bool konvertierbar, das ist eine eingebaute Konvertierung.</p>
<p>Der Compiler sieht bei den beiden op&lt;&lt;-Kandidaten für Date die Konvertierung char* -&gt; bool -&gt; Date und für String die Konvertierung char* -&gt; String</p>
<p>Dass bei der Konvertierung in ein Date noch eine eingebaute Konvertierung nötig ist, macht nichts, dem Compiler ist beides eine User defined conversion.</p>
<p>Abgesehen davon, den Ctor explicit zu machen, könntest du noch überlegen, eine Proxy-Klasse für bool zu schreiben und statt des bool dann die als Parameter zu nehmen:</p>
<pre><code class="language-cpp">struct StrongBool
{
  StrongBool(bool b) : value(b) {}
  bool value;
};

class Date {
public:
  Date(StrongBool readCurrent = false ) { }
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2217757</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217757</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Thu, 31 May 2012 13:22:26 GMT</pubDate></item><item><title><![CDATA[Reply to Impliziter Cast von char* nach bool on Thu, 31 May 2012 13:37:38 GMT]]></title><description><![CDATA[<p>Noch besser wäre natürlich den bool Parameter weg zu werfen. Date(false) sagt nicht viel aus. Viel cooler wäre hier zB ein kompletter wegfall eines etwaigen parameters oder wenn er doch notwendig ist einen enum nehmen.</p>
<p>Ich weiß leider nicht was der Parameter macht deshalb kann ich keine besseren Vorschläge bringen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217768</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217768</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Thu, 31 May 2012 13:37:38 GMT</pubDate></item><item><title><![CDATA[Reply to Impliziter Cast von char* nach bool on Thu, 31 May 2012 14:24:42 GMT]]></title><description><![CDATA[<p>enum <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /><br />
bool ist da irgendwie nicht so toll.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2217790</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217790</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Thu, 31 May 2012 14:24:42 GMT</pubDate></item><item><title><![CDATA[Reply to Impliziter Cast von char* nach bool on Thu, 31 May 2012 16:30:41 GMT]]></title><description><![CDATA[<p>Hört sich gut an. Ich werds so machen, dass die Date-Klasse statt des bool-Parameters ein enum bekommt. Das ist dann auch aussagekräftiger und die Konvertierung verschwindet automatisch.</p>
<p>Danke für die Hilfe! <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/2217844</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2217844</guid><dc:creator><![CDATA[ChristophLu]]></dc:creator><pubDate>Thu, 31 May 2012 16:30:41 GMT</pubDate></item></channel></rss>