<?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[string::operator[]]]></title><description><![CDATA[<p>Tagchen,<br />
ich habe eine kleine String-Klasse mit std::string als Basis geschrieben, um eine bisherige, auf einem char-array basierende zu ersetzen.</p>
<p>Mein Problem ist aktuell, dass Geschichten wie</p>
<pre><code class="language-cpp">if(stringinstanz[0])
</code></pre>
<p>mit Fehler abbrechen.</p>
<p>Die Struktur meiner Klasse ist diese:</p>
<pre><code class="language-cpp">class pstring : public string
{
	public:
		pstring();
		pstring( const char *str );
		pstring( const string &amp;str );
		~pstring();

		int caseCmp( const pstring &amp;str, unsigned int len=pstring::npos );
		int strcmp( const pstring &amp;str, unsigned int len=pstring::npos );

		pstring token( unsigned int pos, bool rest=false, const pstring &amp;sep=&quot; &quot; ) const;
		unsigned int split( const pstring &amp;delim, vector&lt;pstring&gt; &amp;ret, bool allowEmpty ) const;

		operator bool() const;
		operator const char*() const;
};
</code></pre>
<p>So, mein Problem ist nun, dass der standard operator[] nicht funktioniert:</p>
<pre><code class="language-cpp">pstring name(&quot;+lx&quot;);
if(name[0] == '+' || name[0] == '-') {}
</code></pre>
<pre><code>class-client.cpp: In member function »void client::checkMyHost(const char*, bool)«:
class-client.cpp:32: Fehler: ISO C++ besagt, dass diese mehrdeutig sind, auch wenn die schlechteste Umwandlung für das erste besser ist als die schlechteste Umwandlung für das zweite:
/usr/include/c++/4.3/bits/basic_string.h:718: Anmerkung: Kandidat 1: typename _Alloc::rebind&lt;_CharT&gt;::other::reference std::basic_string&lt;_CharT, _Traits, _Alloc&gt;::operator[](typename _Alloc::rebind&lt;_CharT&gt;::other::size_type) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Alloc = std::allocator&lt;char&gt;]
class-client.cpp:32: Anmerkung: Kandidat 2: operator[](const char*, int) &lt;eingebaut&gt;
</code></pre>
<p>Selber den Operator ueberladen funktioniert selbstverfreilich auch nicht.<br />
Was kann man hier machen?<br />
C167</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/223689/string-operator</link><generator>RSS for Node</generator><lastBuildDate>Sat, 03 Oct 2026 16:51:01 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/223689.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 29 Sep 2008 16:02:20 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to string::operator[] on Mon, 29 Sep 2008 16:04:14 GMT]]></title><description><![CDATA[<p>Tagchen,<br />
ich habe eine kleine String-Klasse mit std::string als Basis geschrieben, um eine bisherige, auf einem char-array basierende zu ersetzen.</p>
<p>Mein Problem ist aktuell, dass Geschichten wie</p>
<pre><code class="language-cpp">if(stringinstanz[0])
</code></pre>
<p>mit Fehler abbrechen.</p>
<p>Die Struktur meiner Klasse ist diese:</p>
<pre><code class="language-cpp">class pstring : public string
{
	public:
		pstring();
		pstring( const char *str );
		pstring( const string &amp;str );
		~pstring();

		int caseCmp( const pstring &amp;str, unsigned int len=pstring::npos );
		int strcmp( const pstring &amp;str, unsigned int len=pstring::npos );

		pstring token( unsigned int pos, bool rest=false, const pstring &amp;sep=&quot; &quot; ) const;
		unsigned int split( const pstring &amp;delim, vector&lt;pstring&gt; &amp;ret, bool allowEmpty ) const;

		operator bool() const;
		operator const char*() const;
};
</code></pre>
<p>So, mein Problem ist nun, dass der standard operator[] nicht funktioniert:</p>
<pre><code class="language-cpp">pstring name(&quot;+lx&quot;);
if(name[0] == '+' || name[0] == '-') {}
</code></pre>
<pre><code>class-client.cpp: In member function »void client::checkMyHost(const char*, bool)«:
class-client.cpp:32: Fehler: ISO C++ besagt, dass diese mehrdeutig sind, auch wenn die schlechteste Umwandlung für das erste besser ist als die schlechteste Umwandlung für das zweite:
/usr/include/c++/4.3/bits/basic_string.h:718: Anmerkung: Kandidat 1: typename _Alloc::rebind&lt;_CharT&gt;::other::reference std::basic_string&lt;_CharT, _Traits, _Alloc&gt;::operator[](typename _Alloc::rebind&lt;_CharT&gt;::other::size_type) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Alloc = std::allocator&lt;char&gt;]
class-client.cpp:32: Anmerkung: Kandidat 2: operator[](const char*, int) &lt;eingebaut&gt;
</code></pre>
<p>Selber den Operator ueberladen funktioniert selbstverfreilich auch nicht.<br />
Was kann man hier machen?<br />
C167</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1590376</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1590376</guid><dc:creator><![CDATA[C167]]></dc:creator><pubDate>Mon, 29 Sep 2008 16:04:14 GMT</pubDate></item><item><title><![CDATA[Reply to string::operator[] on Mon, 29 Sep 2008 16:17:15 GMT]]></title><description><![CDATA[<p>Standardmäßig wird kein operator[] erzeugt. Das mußt du schon selber machen wenn du ihn haben willst. Wo ist hier das Problem?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1590394</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1590394</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Mon, 29 Sep 2008 16:17:15 GMT</pubDate></item><item><title><![CDATA[Reply to string::operator[] on Mon, 29 Sep 2008 16:30:02 GMT]]></title><description><![CDATA[<p>Das Problem ist folgendes:<br />
Bei Verwendung von</p>
<pre><code class="language-cpp">const char operator[]( size_t pos ) const
{
	return at( pos );
}
</code></pre>
<p>fuehrt zu:</p>
<pre><code>class-client.cpp: In member function »void client::checkMyHost(const char*, bool)«:
class-client.cpp:32: Fehler: ISO C++ besagt, dass diese mehrdeutig sind, auch wenn die schlechteste Umwandlung für das erste besser ist als die schlechteste Umwandlung für das zweite:
pstring.h:52: Anmerkung: Kandidat 1: const char pstring::operator[](size_t) const
class-client.cpp:32: Anmerkung: Kandidat 2: operator[](const char*, int) &lt;eingebaut&gt;
</code></pre>
<p>Hab ich etwa falsch Ueberladen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1590402</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1590402</guid><dc:creator><![CDATA[C167]]></dc:creator><pubDate>Mon, 29 Sep 2008 16:30:02 GMT</pubDate></item><item><title><![CDATA[Reply to string::operator[] on Mon, 29 Sep 2008 16:37:53 GMT]]></title><description><![CDATA[<p>Stopp, ich hatte übersehen, dass du von string ableitest. Dann hast du natürlich deinen operator[] schon. Ableiten ist hier eigentlich nicht üblich, da der destruktor von string nicht virtual ist.<br />
Was dieser operator[](const char*, int) sein soll weiß ich jetzt auch nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1590409</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1590409</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Mon, 29 Sep 2008 16:37:53 GMT</pubDate></item><item><title><![CDATA[Reply to string::operator[] on Mon, 29 Sep 2008 19:17:56 GMT]]></title><description><![CDATA[<p>Vermutung: Kann es sein, dass</p>
<pre><code class="language-cpp">operator const char*() const;
</code></pre>
<p>dazu führt, dass der Compiler nicht mehr weiß, ob er jetzt in <code>char*</code> umwandeln soll und darauf den <code>Operator []</code> anwenden soll, oder ob er den Operator direkt auf die Klasse anwenden soll. Probiere mal aus, ob es klappt, wenn du den impliziten Cast weg lässt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1590411</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1590411</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 29 Sep 2008 19:17:56 GMT</pubDate></item><item><title><![CDATA[Reply to string::operator[] on Mon, 29 Sep 2008 16:46:28 GMT]]></title><description><![CDATA[<p>Nur um sicher zu gehen: Operatoren werden alle uebernommen (public,protected,private natuerlich respektiert)? Mir wird naemlich alle Nase lang was anderes gesagt <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>Vllt helfen ein paar Infos ueber mein System:<br />
Debian GNU/Linux SID<br />
g++ (Debian 4.3.2-1) 4.3.2</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1590412</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1590412</guid><dc:creator><![CDATA[C167]]></dc:creator><pubDate>Mon, 29 Sep 2008 16:46:28 GMT</pubDate></item><item><title><![CDATA[Reply to string::operator[] on Mon, 29 Sep 2008 17:04:26 GMT]]></title><description><![CDATA[<p>ProgChild schrieb:</p>
<blockquote>
<p>Vermutung: Kann es sein, dass</p>
<pre><code class="language-cpp">operator const char*() const;
</code></pre>
<p>dazu führt, dass der Compiler nicht mehr weiß, ob er jetzt in <code>char*</code> umwandeln soll und darauf den <code>Operator []</code> anwenden soll, oder ob er den Operator direkt auf die Klasse anwenden soll. Probiere mal aus, ob es klappt, wenn du den impliziten Case weg lässt.</p>
</blockquote>
<p>Aehm ja. Problem ist hierbei, dass es eben sehr viele casts gibt, die man nun durch &quot;.c_str()&quot; ersetzen muss, darum hab ich diesen cast ja ueberladen <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 />
Wenn ich durch die zig Zeilen Fehlermeldungen richtig durchsteige, moniert er nur noch die nun fehlende Umwandlung in &quot;const char *&quot;. Aber schon interessant, wieder was gelernt.<br />
Vielen Dank soweit <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>
<p>Ich werd mich mal dran machen, dieses ersetzen vorzunehmen, Kaffee ist vorhanden <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>
<p>Bevor ichs vergesse: Die Standard-Implementierung sieht ja so aus:</p>
<pre><code class="language-cpp">const char&amp; operator[] ( size_t pos ) const;
      char&amp; operator[] ( size_t pos );
</code></pre>
<p>Was hat das zusaetzliche <em>const</em> am Ende der ersten Zeile zu suchen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1590428</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1590428</guid><dc:creator><![CDATA[C167]]></dc:creator><pubDate>Mon, 29 Sep 2008 17:04:26 GMT</pubDate></item><item><title><![CDATA[Reply to string::operator[] on Mon, 29 Sep 2008 17:08:30 GMT]]></title><description><![CDATA[<p>Die eine Funktion ist für konstante Instanzen deiner Klasse die andere für nicht konstante.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1590429</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1590429</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Mon, 29 Sep 2008 17:08:30 GMT</pubDate></item><item><title><![CDATA[Reply to string::operator[] on Mon, 29 Sep 2008 17:09:47 GMT]]></title><description><![CDATA[<p>C167 schrieb:</p>
<blockquote>
<p>Bevor ichs vergesse: Die Standard-Implementierung sieht ja so aus:</p>
<pre><code class="language-cpp">const char&amp; operator[] ( size_t pos ) const;
      char&amp; operator[] ( size_t pos );
</code></pre>
<p>Was hat das zusaetzliche <em>const</em> am Ende der ersten Zeile zu suchen?</p>
</blockquote>
<p>Das ist die Version, die aufgerufen wird, wenn der String selbst `const` ist, also in folgendem Fall:</p>
<pre><code class="language-cpp">string const hi = &quot;Hello, world!&quot;;
cout &lt;&lt; hi[0];
</code></pre>
<p>Hier ist also quasi der `this`-Zeiger ein Zeiger auf ein konstantes Objekt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1590431</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1590431</guid><dc:creator><![CDATA[Konrad Rudolph]]></dc:creator><pubDate>Mon, 29 Sep 2008 17:09:47 GMT</pubDate></item><item><title><![CDATA[Reply to string::operator[] on Mon, 29 Sep 2008 17:17:52 GMT]]></title><description><![CDATA[<p>vielen Dank euch allen <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>
<p>Schlussendlich wird hier wohl (mal wieder) ein Patch von ueber 2000 Zeilen rauskommen, die Entwickler, denen ich den Patch senden werde, werden sich freuen <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/1590441</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1590441</guid><dc:creator><![CDATA[C167]]></dc:creator><pubDate>Mon, 29 Sep 2008 17:17:52 GMT</pubDate></item></channel></rss>