<?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[Konstante Strings in Klassedeklaration definieren]]></title><description><![CDATA[<p>Hi</p>
<p>in meinen Klassen habe ich oft Konstanten (meistens Strings), die ich gerne in der Klasse initialisieren würde (übersichtlicher);</p>
<pre><code class="language-cpp">class Foo {
private:
   static const std::string foo = &quot;Value&quot;;

};
</code></pre>
<p>Leider geht das ja so nicht. Geht das dennoch irgendwie in C++?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/289684/konstante-strings-in-klassedeklaration-definieren</link><generator>RSS for Node</generator><lastBuildDate>Tue, 18 Aug 2026 23:52:00 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/289684.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 10 Jul 2011 23:02:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Konstante Strings in Klassedeklaration definieren on Sun, 10 Jul 2011 23:02:12 GMT]]></title><description><![CDATA[<p>Hi</p>
<p>in meinen Klassen habe ich oft Konstanten (meistens Strings), die ich gerne in der Klasse initialisieren würde (übersichtlicher);</p>
<pre><code class="language-cpp">class Foo {
private:
   static const std::string foo = &quot;Value&quot;;

};
</code></pre>
<p>Leider geht das ja so nicht. Geht das dennoch irgendwie in C++?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2091193</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2091193</guid><dc:creator><![CDATA[C++XX]]></dc:creator><pubDate>Sun, 10 Jul 2011 23:02:12 GMT</pubDate></item><item><title><![CDATA[Reply to Konstante Strings in Klassedeklaration definieren on Mon, 11 Jul 2011 00:21:17 GMT]]></title><description><![CDATA[<p>statische member können nur außerhalb der klasse definiert werden</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2091200</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2091200</guid><dc:creator><![CDATA[jkzu76]]></dc:creator><pubDate>Mon, 11 Jul 2011 00:21:17 GMT</pubDate></item><item><title><![CDATA[Reply to Konstante Strings in Klassedeklaration definieren on Mon, 11 Jul 2011 05:54:58 GMT]]></title><description><![CDATA[<p>Was ist denn der Sinn dieser Konstante? Da Du sie privat deklariert hast, kann sie doch eh keiner verwenden. Warum also überhaupt in der Klassendefinition erwähnen? Du kannst die Deklaration und Definition komplett in Deine CPP-Datei verlagern.</p>
<p>Und wie ich anderswo schon erwähnt hatte: Für String-<em>Konstanten</em> solltest Du bei char* oder char[] bleiben. Für alles andere nimmst Du std::string.</p>
<p>Du kannst es so machen:</p>
<pre><code class="language-cpp">// header
#include &lt;iostream&gt;
#include &lt;ostream&gt;

class foo {
  static const char message[]; // Deklaration, unvollständiger Typ
public:
  void operator()() const {
    std::cout &lt;&lt; (message+0) &lt;&lt; std::endl;
  }
};

// cpp-Datei
#include &quot;foo.hpp&quot;

const char foo::message[] = &quot;Dies und das&quot;;
</code></pre>
<p>oder ganz aus dem Header entfernen:</p>
<pre><code class="language-cpp">// header
class foo {
public:
  void operator()() const;
};

// cpp-Datei
#include &lt;iostream&gt;
#include &lt;ostream&gt;
#include &quot;foo.hpp&quot;

const char message[] = &quot;Dies und das&quot;;

void foo::operator()() const {
  std::cout &lt;&lt; message &lt;&lt; std::endl;
}
</code></pre>
<p>Also: Statische Datenelemente kann man in einer Klassendefinition nur <em>deklarieren</em>. Definieren nur außerhalb.</p>
<p>Am Rande: Include-Guards nicht vergessen. Die habe ich hier einfach mal weggelassen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2091219</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2091219</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Mon, 11 Jul 2011 05:54:58 GMT</pubDate></item><item><title><![CDATA[Reply to Konstante Strings in Klassedeklaration definieren on Mon, 11 Jul 2011 11:52:03 GMT]]></title><description><![CDATA[<p>Wieso char[]? Mit char* wäre wenigstens der Typ überall bekannt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2091341</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2091341</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 11 Jul 2011 11:52:03 GMT</pubDate></item><item><title><![CDATA[Reply to Konstante Strings in Klassedeklaration definieren on Mon, 11 Jul 2011 12:03:41 GMT]]></title><description><![CDATA[<p>hustbaer schrieb:</p>
<blockquote>
<p>Wieso char[]? Mit char* wäre wenigstens der Typ überall bekannt.</p>
</blockquote>
<p>Ist er doch auch so (soweit notwendig). Ich sehe hier keinen Nachteil bei Verwendung von char[] - potentiell ist das sogar besser, weil ein Verweis auf das Objekt immer unmittelbar auf den String zeigt, und nicht den Umweg über ein zusätzliches Zeigerobjekt gehen muss.<br />
Zwar ist char[] ein unvollständiger Typ, er transportiert aber nicht weniger Information, als as char* täte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2091344</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2091344</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 11 Jul 2011 12:03:41 GMT</pubDate></item><item><title><![CDATA[Reply to Konstante Strings in Klassedeklaration definieren on Mon, 11 Jul 2011 19:45:21 GMT]]></title><description><![CDATA[<p>Hm.<br />
D.h. man kann die (implizite) Array-To-Pointer Konvertierung verwenden, obwohl die Grösse des Arrays noch nicht bekannt ist?<br />
Spricht natürlich im Prinzip nix dagegen, ich wusste nur nicht dass das erlaubt ist.</p>
<p>Wobei ja, jetzt wo ich nochmal drüber nachdenke... ist eigentlich logisch.</p>
<p>Einwand zurückgezogen <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/2091539</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2091539</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 11 Jul 2011 19:45:21 GMT</pubDate></item></channel></rss>