<?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[const Atribute in einer klasse]]></title><description><![CDATA[<pre><code class="language-cpp">class Klasse
{
	private:
		const int scr_max_x = 80; //Geht nicht
		const int scr_max_y = 25; //Geht nicht
		int punkte;
		int ziel_neu;

	public:
		CKlasse();

		int getZiel_X(void);
		void SetZiel_X(int wert);
		int getZiel_Y(void);
		void setZiel_Y(int wert);

		void setZiel(void);
};
</code></pre>
<p>das ist ein ausschnitt....<br />
nun meine frage wie kann ich konstante Attribute deklarieren ... ???<br />
const int scr_max_x = 80;<br />
const int scr_max_y = 25;<br />
wenn ich compielere für die o.g. zeilen kommt folgende error meldung..<br />
Cannot initialize a class member here</p>
<p>wie kann man das sonst noch realisieren ????</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/153584/const-atribute-in-einer-klasse</link><generator>RSS for Node</generator><lastBuildDate>Mon, 07 Sep 2026 14:06:40 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/153584.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 18 Jul 2006 16:33:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to const Atribute in einer klasse on Tue, 18 Jul 2006 16:34:41 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">class Klasse
{
	private:
		const int scr_max_x = 80; //Geht nicht
		const int scr_max_y = 25; //Geht nicht
		int punkte;
		int ziel_neu;

	public:
		CKlasse();

		int getZiel_X(void);
		void SetZiel_X(int wert);
		int getZiel_Y(void);
		void setZiel_Y(int wert);

		void setZiel(void);
};
</code></pre>
<p>das ist ein ausschnitt....<br />
nun meine frage wie kann ich konstante Attribute deklarieren ... ???<br />
const int scr_max_x = 80;<br />
const int scr_max_y = 25;<br />
wenn ich compielere für die o.g. zeilen kommt folgende error meldung..<br />
Cannot initialize a class member here</p>
<p>wie kann man das sonst noch realisieren ????</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1099919</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1099919</guid><dc:creator><![CDATA[CPPKILLER]]></dc:creator><pubDate>Tue, 18 Jul 2006 16:34:41 GMT</pubDate></item><item><title><![CDATA[Reply to const Atribute in einer klasse on Tue, 18 Jul 2006 16:35:36 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">static const int scr_max_x = 80;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1099921</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1099921</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Tue, 18 Jul 2006 16:35:36 GMT</pubDate></item><item><title><![CDATA[Reply to const Atribute in einer klasse on Tue, 18 Jul 2006 16:43:19 GMT]]></title><description><![CDATA[<p>Michael E. schrieb:</p>
<blockquote>
<pre><code class="language-cpp">static const int scr_max_x = 80;
</code></pre>
</blockquote>
<p>THX<br />
kannst du mit paar sätzen erklären was dieses &quot;static&quot; bewirken soll, was das ist ????</p>
<p>edit: die selbe fehlermeldung bekomme habe ich auch mit static bekommen.<br />
und wenn ich doppel klick auf die fehlermeldung mache, springt er sofot hinter zuweisungs operator.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1099924</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1099924</guid><dc:creator><![CDATA[CPPKILLER]]></dc:creator><pubDate>Tue, 18 Jul 2006 16:43:19 GMT</pubDate></item><item><title><![CDATA[Reply to const Atribute in einer klasse on Tue, 18 Jul 2006 16:50:43 GMT]]></title><description><![CDATA[<p>CPPKILLER schrieb:</p>
<blockquote>
<p>edit: die selbe fehlermeldung bekomme habe ich auch mit static bekommen.<br />
und wenn ich doppel klick auf die fehlermeldung mache, springt er sofot hinter zuweisungs operator.</p>
</blockquote>
<p>Welchen Compiler benutzt du? Was genau hast du geschrieben?</p>
<blockquote>
<p>Michael E. schrieb:</p>
<blockquote>
<pre><code class="language-cpp">static const int scr_max_x = 80;
</code></pre>
</blockquote>
<p>THX<br />
kannst du mit paar sätzen erklären was dieses &quot;static&quot; bewirken soll, was das ist ????</p>
</blockquote>
<p>static bedeutet in diesem Zusammenhang, dass die Membervariable nur ein einziges Mal erzeugt wird, egal wie viele Instanzen der Klasse du erstellst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1099929</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1099929</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Tue, 18 Jul 2006 16:50:43 GMT</pubDate></item><item><title><![CDATA[Reply to const Atribute in einer klasse on Tue, 18 Jul 2006 17:02:39 GMT]]></title><description><![CDATA[<p>Hallo</p>
<p>Du kannst die Variablen, ja auch mit Hilfe der Initialisierungsliste im C'tor initialisieren.</p>
<p>chrische</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1099934</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1099934</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Tue, 18 Jul 2006 17:02:39 GMT</pubDate></item><item><title><![CDATA[Reply to const Atribute in einer klasse on Tue, 18 Jul 2006 17:14:07 GMT]]></title><description><![CDATA[<p>chrische5 schrieb:</p>
<blockquote>
<p>Hallo</p>
<p>Du kannst die Variablen, ja auch mit Hilfe der Initialisierungsliste im C'tor initialisieren.</p>
<p>chrische</p>
</blockquote>
<p>ja aber ich will 2 arrays anlegen und die array größe muss ja const sein.</p>
<p>z.b. array[groesse]; //groesse muss ja dann const sein oder ???</p>
<p>ich benutze Borland C++ 4.5 ist zwar alt, reicht aber für das was ich brauche..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1099941</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1099941</guid><dc:creator><![CDATA[CPPKILLER]]></dc:creator><pubDate>Tue, 18 Jul 2006 17:14:07 GMT</pubDate></item><item><title><![CDATA[Reply to const Atribute in einer klasse on Tue, 18 Jul 2006 17:27:33 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">class CTest
{
private:
    const int groesse = 10;
    int x, y;
    int array[groesse]; // &lt;-- Für son feld brauche ich die konstante(n)
public:
    CTest();

    int getX(void);
    ...
};
</code></pre>
<p>das ist jetzt son beispiel wo ich soeine fehlermeldung bekomme.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1099949</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1099949</guid><dc:creator><![CDATA[CPPKILLER]]></dc:creator><pubDate>Tue, 18 Jul 2006 17:27:33 GMT</pubDate></item><item><title><![CDATA[Reply to const Atribute in einer klasse on Tue, 18 Jul 2006 17:29:19 GMT]]></title><description><![CDATA[<p>Mit static const int funktioniert das genau so. Wenn Du einen Fehler bekommst (bekomme ich mit VC++6 z.B. auch), ist der Compiler zu alt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1099950</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1099950</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Tue, 18 Jul 2006 17:29:19 GMT</pubDate></item><item><title><![CDATA[Reply to const Atribute in einer klasse on Tue, 18 Jul 2006 17:37:34 GMT]]></title><description><![CDATA[<p>LordJaxom schrieb:</p>
<blockquote>
<p>Mit static const int funktioniert das genau so. Wenn Du einen Fehler bekommst (bekomme ich mit VC++6 z.B. auch), ist der Compiler zu alt.</p>
</blockquote>
<p>was fürn compiler würdest du mir empfehlen... ?<br />
ich benutze borland C++ 4.5 wegen BGI, weil wir das in der schule brauchen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1099955</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1099955</guid><dc:creator><![CDATA[CPPKILLER]]></dc:creator><pubDate>Tue, 18 Jul 2006 17:37:34 GMT</pubDate></item><item><title><![CDATA[Reply to const Atribute in einer klasse on Tue, 18 Jul 2006 17:42:21 GMT]]></title><description><![CDATA[<p>Verwende den Enum-Hack.<br />
<a href="http://www.cs.usyd.edu.au/~sholden/TEACHING/PP/2000summer/RESOURCES/TIC/tic0095.html" rel="nofollow">http://www.cs.usyd.edu.au/~sholden/TEACHING/PP/2000summer/RESOURCES/TIC/tic0095.html</a></p>
<blockquote>
<p>In older versions of C++, static const was not supported inside classes. This meant that const was useless for constant expressions inside classes. However, people still wanted to do this so a common solution (typically referred to as the “enum hack”) was to use an untagged enum with no instances. An enumeration must have all its values established at compile time, it’s local to the class, and its values are available for constant expressions. ...</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/1099956</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1099956</guid><dc:creator><![CDATA[Erhard Henkes]]></dc:creator><pubDate>Tue, 18 Jul 2006 17:42:21 GMT</pubDate></item><item><title><![CDATA[Reply to const Atribute in einer klasse on Tue, 18 Jul 2006 20:03:48 GMT]]></title><description><![CDATA[<p>Hallo</p>
<p>CPPKILLER schrieb:</p>
<blockquote>
<p>chrische5 schrieb:</p>
<blockquote>
<p>Hallo</p>
<p>Du kannst die Variablen, ja auch mit Hilfe der Initialisierungsliste im C'tor initialisieren.</p>
<p>chrische</p>
</blockquote>
<p>ja aber ich will 2 arrays anlegen und die array größe muss ja const sein.</p>
<p>z.b. array[groesse]; //groesse muss ja dann const sein oder ???</p>
<p>ich benutze Borland C++ 4.5 ist zwar alt, reicht aber für das was ich brauche..</p>
</blockquote>
<p>Du kannst doch auch einfach std::vector benutzen. Da haste ein dynamisches array und kannst die Größe jederzeit bestimmen und ändern.</p>
<p>chrische</p>
<p>chrische</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1100046</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1100046</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Tue, 18 Jul 2006 20:03:48 GMT</pubDate></item><item><title><![CDATA[Reply to const Atribute in einer klasse on Tue, 18 Jul 2006 21:26:02 GMT]]></title><description><![CDATA[<p>CPPKILLER schrieb:</p>
<blockquote>
<p>ja aber ich will 2 arrays anlegen und die array größe muss ja const sein.</p>
<p>z.b. array[groesse]; //groesse muss ja dann const sein oder ???</p>
</blockquote>
<p>Nicht const. In den Klammern muss ein konstanter Ausdruck stehen. Das ist ein kleiner aber feiner Unterschied.<br />
Mach es, wie Erhard schon sagte, mit dem enum Hack.</p>
<pre><code class="language-cpp">private:
        enum
        {
            scr_max_x = 80,
            scr_max_y = 25
        };
</code></pre>
<p>Nicht wirklich schön, bei älteren Compilern geht's aber leider nicht anders.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1100074</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1100074</guid><dc:creator><![CDATA[groovemaster]]></dc:creator><pubDate>Tue, 18 Jul 2006 21:26:02 GMT</pubDate></item><item><title><![CDATA[Reply to const Atribute in einer klasse on Wed, 19 Jul 2006 10:26:26 GMT]]></title><description><![CDATA[<p>groovemaster schrieb:</p>
<blockquote>
<pre><code class="language-cpp">private:
        enum
        {
            scr_max_x = 80,
            scr_max_y = 25
        };
</code></pre>
<p>Nicht wirklich schön, bei älteren Compilern geht's aber leider nicht anders.</p>
</blockquote>
<p>Danke <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="😉"
    /> so hats funktioniert <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/1100281</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1100281</guid><dc:creator><![CDATA[CPPKILLER]]></dc:creator><pubDate>Wed, 19 Jul 2006 10:26:26 GMT</pubDate></item></channel></rss>