<?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[statische Klassenelemente]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich würde gerne statische Klassenelemente benutzen, erhalte aber beim Kompilieren von</p>
<pre><code class="language-cpp">class a
{
	static long count;
	a();
};

a::a()
{
	count++;	
}
</code></pre>
<p>die Meldung &quot;undefined reference to `a::count'&quot;, wohingegen</p>
<pre><code class="language-cpp">class a
{
	static long count;
	a() { count++; }
};
</code></pre>
<p>fehlerfrei kompiliert (ich benutze gcc unter Linux). Weiss jemand, woran das liegt und wie ich es behebe?</p>
<p>Des Weiteren bekomme ich beim Kompilieren von</p>
<pre><code class="language-cpp">class b
{
	static long count = 0;
};
</code></pre>
<p>die Meldung &quot;ISO C++ forbids in-class initialization of non-const static member ‘count’&quot;. Gibt es eine Möglichkeit count zu initialisieren, ohne dass man b zu einer nicht-statischen Klasse machen muss?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/194347/statische-klassenelemente</link><generator>RSS for Node</generator><lastBuildDate>Mon, 28 Sep 2026 11:32:01 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/194347.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 05 Oct 2007 13:45:18 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to statische Klassenelemente on Fri, 05 Oct 2007 13:45:18 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich würde gerne statische Klassenelemente benutzen, erhalte aber beim Kompilieren von</p>
<pre><code class="language-cpp">class a
{
	static long count;
	a();
};

a::a()
{
	count++;	
}
</code></pre>
<p>die Meldung &quot;undefined reference to `a::count'&quot;, wohingegen</p>
<pre><code class="language-cpp">class a
{
	static long count;
	a() { count++; }
};
</code></pre>
<p>fehlerfrei kompiliert (ich benutze gcc unter Linux). Weiss jemand, woran das liegt und wie ich es behebe?</p>
<p>Des Weiteren bekomme ich beim Kompilieren von</p>
<pre><code class="language-cpp">class b
{
	static long count = 0;
};
</code></pre>
<p>die Meldung &quot;ISO C++ forbids in-class initialization of non-const static member ‘count’&quot;. Gibt es eine Möglichkeit count zu initialisieren, ohne dass man b zu einer nicht-statischen Klasse machen muss?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1378638</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1378638</guid><dc:creator><![CDATA[ingobulla]]></dc:creator><pubDate>Fri, 05 Oct 2007 13:45:18 GMT</pubDate></item><item><title><![CDATA[Reply to statische Klassenelemente on Fri, 05 Oct 2007 14:00:38 GMT]]></title><description><![CDATA[<p>Header (b.h):</p>
<pre><code class="language-cpp">class b
{
    static long count;
};
</code></pre>
<p>Source (b.cpp):</p>
<pre><code class="language-cpp">#include &quot;b.h&quot;

long b::count = 0;
</code></pre>
<p>cu André</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1378653</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1378653</guid><dc:creator><![CDATA[asc]]></dc:creator><pubDate>Fri, 05 Oct 2007 14:00:38 GMT</pubDate></item><item><title><![CDATA[Reply to statische Klassenelemente on Fri, 05 Oct 2007 16:41:47 GMT]]></title><description><![CDATA[<p>Danke, aber gibt es da auch eine sauberere Lösung? So muss ich ja, wenn ich die Klasse b verwenden will, erstmal</p>
<pre><code class="language-cpp">long b::count = 0;
</code></pre>
<p>ausführen. Das scheint mir gegen das Prinzip der Verkapselung zu verstoßen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1378756</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1378756</guid><dc:creator><![CDATA[ingobulla]]></dc:creator><pubDate>Fri, 05 Oct 2007 16:41:47 GMT</pubDate></item><item><title><![CDATA[Reply to statische Klassenelemente on Fri, 05 Oct 2007 16:48:59 GMT]]></title><description><![CDATA[<p>Das <em>ist</em> die saubere Lösung und es verstößt keineswegs gegen das Prinzip der Datenkapselung.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1378762</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1378762</guid><dc:creator><![CDATA[finix]]></dc:creator><pubDate>Fri, 05 Oct 2007 16:48:59 GMT</pubDate></item><item><title><![CDATA[Reply to statische Klassenelemente on Fri, 05 Oct 2007 17:20:09 GMT]]></title><description><![CDATA[<p>Mein Problem mit dieser Lösung ist folgendes: Angenommen b hat die Form</p>
<pre><code class="language-cpp">class b
{
    static long count;

    static int f(int x);
    static int g(int x);
};
</code></pre>
<p>und f und g ändern count und ihr Rückgabewert hängt von count ab.</p>
<p>Dann müsste ein Nutzer der Klasse b darauf achten, dass, bevor er f oder g benutzt, count korrekt als 0 initialisiert ist. Es wäre aus meiner Sicht wünschenswert, wenn diese Initialisierung automatisch geschieht (wie dies bei nicht-statischen Klassen durch den Konstruktor erfolgt).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1378772</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1378772</guid><dc:creator><![CDATA[ingobulla]]></dc:creator><pubDate>Fri, 05 Oct 2007 17:20:09 GMT</pubDate></item><item><title><![CDATA[Reply to statische Klassenelemente on Fri, 05 Oct 2007 17:39:54 GMT]]></title><description><![CDATA[<p>Wenn du es genauso machst, wie asc es dir geschrieben hat, IST count mit 0 initialisiert. Du führst long b::count = 0; ja nicht aus, es ist Teil der Klassendefinitionen in source.cpp.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1378790</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1378790</guid><dc:creator><![CDATA[mad_martin]]></dc:creator><pubDate>Fri, 05 Oct 2007 17:39:54 GMT</pubDate></item><item><title><![CDATA[Reply to statische Klassenelemente on Sat, 06 Oct 2007 10:59:09 GMT]]></title><description><![CDATA[<p>Danke, jetzt verstehe ich.</p>
<p>Aber hat jemand noch etwas zu meinem ersten Problem (der erste Text in diesem Eintrag, Klasse a liefert &quot;undefined reference to `a::count'&quot;) zu sagen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1379064</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1379064</guid><dc:creator><![CDATA[ingobulla]]></dc:creator><pubDate>Sat, 06 Oct 2007 10:59:09 GMT</pubDate></item><item><title><![CDATA[Reply to statische Klassenelemente on Sat, 06 Oct 2007 11:05:51 GMT]]></title><description><![CDATA[<p>ingobulla schrieb:</p>
<blockquote>
<p>Danke, jetzt verstehe ich.</p>
<p>Aber hat jemand noch etwas zu meinem ersten Problem (der erste Text in diesem Eintrag, Klasse a liefert &quot;undefined reference to `a::count'&quot;) zu sagen?</p>
</blockquote>
<p>Eine Klassendefinition besteht nur aus Deklarationen, nicht Definitionen. Um ein Objekt nutzen zu können, muss es aber genau einmal im Programm definiert worden sein. Dazu ist die extra Definition notwendig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1379065</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1379065</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sat, 06 Oct 2007 11:05:51 GMT</pubDate></item><item><title><![CDATA[Reply to statische Klassenelemente on Sat, 06 Oct 2007 11:51:59 GMT]]></title><description><![CDATA[<p>Nür für konstante statische Ganzzahlvariablen kannst du in der Deklaration schon einen Wert angeben, ansonsten geht es nur so, wie oben beschrieben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1379084</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1379084</guid><dc:creator><![CDATA[Th]]></dc:creator><pubDate>Sat, 06 Oct 2007 11:51:59 GMT</pubDate></item></channel></rss>