<?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[Vererbung statischer Datenelemente]]></title><description><![CDATA[<p>Hallo,<br />
wahrscheinlich eine ganz dumme Frage aber ich stehe gerade auf dem Schlauch und komm nicht drauf: Wie vererbt man statische Datenelemente? Mein Problem ist, dass ein statisches Datenelement nicht virtual sein kann.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

using namespace std;

class Auto{
public:
  static int AnzahlPS;
  static void showPS(){
    cout &lt;&lt; AnzahlPS &lt;&lt; endl;
  }
};

class AudiA4: public Auto{
};

int AudiA4::AnzahlPS(105);

class FerrariF40: public Auto{
};
int FerrariF40::AnzahlPS(450);

int main(){
  AudiA4::showPS();
  FerrariF40::showPS();
}
</code></pre>
<p>Fehler:</p>
<pre><code>test.cc:16: error: ISO C++ does not permit 'Auto::AnzahlPS' to be defined as 'AudiA4::AnzahlPS'
test.cc:20: error: ISO C++ does not permit 'Auto::AnzahlPS' to be defined as 'FerrariF40::AnzahlPS'
test.cc:20: error: redefinition of 'int Auto::AnzahlPS'
test.cc:16: error: 'int Auto::AnzahlPS' previously defined here
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/246394/vererbung-statischer-datenelemente</link><generator>RSS for Node</generator><lastBuildDate>Fri, 18 Sep 2026 04:35:48 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/246394.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 27 Jul 2009 16:13:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Vererbung statischer Datenelemente on Mon, 27 Jul 2009 16:13:31 GMT]]></title><description><![CDATA[<p>Hallo,<br />
wahrscheinlich eine ganz dumme Frage aber ich stehe gerade auf dem Schlauch und komm nicht drauf: Wie vererbt man statische Datenelemente? Mein Problem ist, dass ein statisches Datenelement nicht virtual sein kann.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

using namespace std;

class Auto{
public:
  static int AnzahlPS;
  static void showPS(){
    cout &lt;&lt; AnzahlPS &lt;&lt; endl;
  }
};

class AudiA4: public Auto{
};

int AudiA4::AnzahlPS(105);

class FerrariF40: public Auto{
};
int FerrariF40::AnzahlPS(450);

int main(){
  AudiA4::showPS();
  FerrariF40::showPS();
}
</code></pre>
<p>Fehler:</p>
<pre><code>test.cc:16: error: ISO C++ does not permit 'Auto::AnzahlPS' to be defined as 'AudiA4::AnzahlPS'
test.cc:20: error: ISO C++ does not permit 'Auto::AnzahlPS' to be defined as 'FerrariF40::AnzahlPS'
test.cc:20: error: redefinition of 'int Auto::AnzahlPS'
test.cc:16: error: 'int Auto::AnzahlPS' previously defined here
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1750392</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1750392</guid><dc:creator><![CDATA[Karl R]]></dc:creator><pubDate>Mon, 27 Jul 2009 16:13:31 GMT</pubDate></item><item><title><![CDATA[Reply to Vererbung statischer Datenelemente on Mon, 27 Jul 2009 17:36:25 GMT]]></title><description><![CDATA[<blockquote>
<p>Wie vererbt man statische Datenelemente?</p>
</blockquote>
<p>So wie du das hier darstellst, ist AnzahlPS nicht statisch. Sie aendert sich ja von Autotyp zu Autotyp.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1750427</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1750427</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Mon, 27 Jul 2009 17:36:25 GMT</pubDate></item><item><title><![CDATA[Reply to Vererbung statischer Datenelemente on Mon, 27 Jul 2009 17:51:58 GMT]]></title><description><![CDATA[<p>knivil schrieb:</p>
<blockquote>
<blockquote>
<p>Wie vererbt man statische Datenelemente?</p>
</blockquote>
<p>So wie du das hier darstellst, ist AnzahlPS nicht statisch. Sie aendert sich ja von Autotyp zu Autotyp.</p>
</blockquote>
<p>Ja, aber für jeden Typ ist der Wert statisch. Oder anders gefragt: Kann ich in den abgeleiteten Klassen statische Daten haben und in der Basisklasse eine Funktion, die diese Daten verwendet? Das Autobeispiel trifft das glaube ich schon recht gut.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1750437</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1750437</guid><dc:creator><![CDATA[Karl R]]></dc:creator><pubDate>Mon, 27 Jul 2009 17:51:58 GMT</pubDate></item><item><title><![CDATA[Reply to Vererbung statischer Datenelemente on Mon, 27 Jul 2009 18:07:11 GMT]]></title><description><![CDATA[<p>Dein Design ist etwas problematisch: Woher soll die Klasse denn wissen, ab wann ein von ihr vererbtes Element statisch ist? Wie ja bereits gesagt wurde, ist es bei der Auto-Klasse noch nicht statisch. Oder was ist, wenn du eine weitere Vererbungsebene einführst, etwa einen AudiA4Turbo?</p>
<p>Vielleicht ist dir mit einem anderen Designansatz geholfen, hier ein möglicher Vorschlag:</p>
<pre><code class="language-cpp">template&lt;class T&gt; class AnzahlPS{
public:
  static int anzahl;
  static void show(){cout&lt;&lt; anzahl &lt;&lt;endl;}
};

class Auto{
};

class AudiA4: public Auto{
};
class FerrariF40: public Auto{
};

template&lt;&gt; int AnzahlPS&lt;AudiA4&gt;::anzahl=105;
template&lt;&gt; int AnzahlPS&lt;FerrariF40&gt;::anzahl=450;

int main(){
  AnzahlPS&lt;AudiA4&gt;().show();
  AnzahlPS&lt;FerrariF40&gt;().show();
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1750444</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1750444</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 27 Jul 2009 18:07:11 GMT</pubDate></item><item><title><![CDATA[Reply to Vererbung statischer Datenelemente on Mon, 27 Jul 2009 18:47:45 GMT]]></title><description><![CDATA[<p>Karl R schrieb:</p>
<blockquote>
<p>knivil schrieb:</p>
<blockquote>
<blockquote>
<p>Wie vererbt man statische Datenelemente?</p>
</blockquote>
<p>So wie du das hier darstellst, ist AnzahlPS nicht statisch. Sie aendert sich ja von Autotyp zu Autotyp.</p>
</blockquote>
<p>Ja, aber für jeden Typ ist der Wert statisch. Oder anders gefragt: Kann ich in den abgeleiteten Klassen statische Daten haben und in der Basisklasse eine Funktion, die diese Daten verwendet? Das Autobeispiel trifft das glaube ich schon recht gut.</p>
</blockquote>
<p>Nein. Jedenfalls nicht, wenn die betreffende Funktion nicht virtuell ist. Auch der Vorschlag von SeppJ beantwortet ja deine Frage nicht, da anzahl nun in einer Klasse ganz außerhalb der Auto-Hierarchie steht.</p>
<p>Warum nicht etwas in der Art:</p>
<pre><code class="language-cpp">class Auto {

public:
   void show() { cout &lt;&lt; anzahlPS() &lt;&lt; std::endl; }

   virtual unsigned anzahlPS() const = 0;

};

class Audi {

public:
   virtual unsigned anzahlPS() const { return 105; }
};

class Ente {

public:
   virtual unsigned anzahlPS() const { return 26; }
};

int main() {
   Audi().show();
   Ente().show();
   return 0;
};
</code></pre>
<p>Stefan.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1750474</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1750474</guid><dc:creator><![CDATA[DStefan]]></dc:creator><pubDate>Mon, 27 Jul 2009 18:47:45 GMT</pubDate></item><item><title><![CDATA[Reply to Vererbung statischer Datenelemente on Mon, 27 Jul 2009 19:16:00 GMT]]></title><description><![CDATA[<p>DStefan schrieb:</p>
<blockquote>
<p>Nein. Jedenfalls nicht, wenn die betreffende Funktion nicht virtuell ist. Auch der Vorschlag von SeppJ beantwortet ja deine Frage nicht, da anzahl nun in einer Klasse ganz außerhalb der Auto-Hierarchie steht.</p>
<p>Warum nicht etwas in der Art:</p>
<p>[...]</p>
</blockquote>
<p>Genau das ist es.</p>
<p>*Mit der Hand vor den Kopf schlag*</p>
<p>Ich wusste doch, dass das die Frage dumm war, da hätte ich mit wenig Nachdenken auch drauf kommen können. Und bei Bedarf kann ich dann in den Klassen die wirklich eine statische PS-Zahl haben, eine statische Veriable einführen die dann von anzahlPS() zurückgegeben wird. Auf die Weise kann man die PS Zahl zum Beispiel aus einer Datei lesen, ohne sie hart im Code zu haben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1750497</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1750497</guid><dc:creator><![CDATA[Karl R]]></dc:creator><pubDate>Mon, 27 Jul 2009 19:16:00 GMT</pubDate></item></channel></rss>