<?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[Frage zu static]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe eine kleine Frage zu static. Ich habe eine Application Klasse mit ausschließlich statischen Elementen. Darunter auch ein Options Objekt, welches ebenfalls statisch ist.</p>
<p>Folgender Code ist mir nicht so ganz klar:</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;

class Options{
public:
  Options(){
    opt1 = false;
  }

  void setOpt1(bool val) {
    opt1=val;
  }

  bool getOpt1() {
    return opt1;
  }
private:
  bool opt1;
};

class Application {
public:
  static Options getOptions(){
    return opts;
  }
  static Options* getOptionsPtr(){
    return &amp;opts;
  }
private:
  static Options opts;
};

Options Application::opts;

int main(){
  Application::getOptions().setOpt1(true);
  if (Application::getOptions().getOpt1()){
    cout &lt;&lt; &quot;opt1 true&quot; &lt;&lt; endl;
  }else{
    cout &lt;&lt; &quot;opt1 false&quot; &lt;&lt; endl;
  }

  Application::getOptionsPtr()-&gt;setOpt1(true);
  if (Application::getOptionsPtr()-&gt;getOpt1()){
    cout &lt;&lt; &quot;opt1 true&quot; &lt;&lt; endl;
  }else{
    cout &lt;&lt; &quot;opt1 false&quot; &lt;&lt; endl;
  }
  return 0;
}
</code></pre>
<p>Greife ich mit der getOptions() Methode zu, klappt es leider nicht wie erwartet.</p>
<p>Frage 1: Warum wird bei getOptions() nicht true zurückgeliefert?<br />
Frage 2: Mit dem Ptr läuft es so wie erwartet, aber sollte man das in dieser Form so machen?</p>
<p>Ausgabe:</p>
<pre><code>opt1 false
opt1 true
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/337050/frage-zu-static</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Apr 2026 13:30:30 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/337050.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 04 Mar 2016 13:33:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Frage zu static on Fri, 04 Mar 2016 13:33:12 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe eine kleine Frage zu static. Ich habe eine Application Klasse mit ausschließlich statischen Elementen. Darunter auch ein Options Objekt, welches ebenfalls statisch ist.</p>
<p>Folgender Code ist mir nicht so ganz klar:</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;

class Options{
public:
  Options(){
    opt1 = false;
  }

  void setOpt1(bool val) {
    opt1=val;
  }

  bool getOpt1() {
    return opt1;
  }
private:
  bool opt1;
};

class Application {
public:
  static Options getOptions(){
    return opts;
  }
  static Options* getOptionsPtr(){
    return &amp;opts;
  }
private:
  static Options opts;
};

Options Application::opts;

int main(){
  Application::getOptions().setOpt1(true);
  if (Application::getOptions().getOpt1()){
    cout &lt;&lt; &quot;opt1 true&quot; &lt;&lt; endl;
  }else{
    cout &lt;&lt; &quot;opt1 false&quot; &lt;&lt; endl;
  }

  Application::getOptionsPtr()-&gt;setOpt1(true);
  if (Application::getOptionsPtr()-&gt;getOpt1()){
    cout &lt;&lt; &quot;opt1 true&quot; &lt;&lt; endl;
  }else{
    cout &lt;&lt; &quot;opt1 false&quot; &lt;&lt; endl;
  }
  return 0;
}
</code></pre>
<p>Greife ich mit der getOptions() Methode zu, klappt es leider nicht wie erwartet.</p>
<p>Frage 1: Warum wird bei getOptions() nicht true zurückgeliefert?<br />
Frage 2: Mit dem Ptr läuft es so wie erwartet, aber sollte man das in dieser Form so machen?</p>
<p>Ausgabe:</p>
<pre><code>opt1 false
opt1 true
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2489340</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2489340</guid><dc:creator><![CDATA[static]]></dc:creator><pubDate>Fri, 04 Mar 2016 13:33:12 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu static on Fri, 04 Mar 2016 13:40:43 GMT]]></title><description><![CDATA[<p>Deine getOptions() Funktion liefert eine Kopie zurück! Daher änderst du mit deinem</p>
<pre><code>Application::getOptions().setOpt1(true);
</code></pre>
<p>nur den Wert der Kopie. Die <code>getOptions()</code> Funktion sollte eine Referenz zurückgeben. Ob Referenz oder Pointer ist aber wohl Geschmackssache.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2489341</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2489341</guid><dc:creator><![CDATA[sebi707]]></dc:creator><pubDate>Fri, 04 Mar 2016 13:40:43 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu static on Fri, 04 Mar 2016 13:41:01 GMT]]></title><description><![CDATA[<p>static schrieb:</p>
<blockquote>
<p>Ich habe eine Application Klasse mit ausschließlich statischen Elementen. Darunter auch ein Options Objekt, welches ebenfalls statisch ist.</p>
</blockquote>
<p>Warum? Schon mal etwas von <em>namespace</em> gehört?</p>
<p>static schrieb:</p>
<blockquote>
<p>Frage 1: Warum wird bei getOptions() nicht true zurückgeliefert?</p>
</blockquote>
<p>Weil getOptions() eine Kopie der Optionen liefert.</p>
<p>static schrieb:</p>
<blockquote>
<p>Frage 2: Mit dem Ptr läuft es so wie erwartet, aber sollte man das in dieser Form so machen?</p>
</blockquote>
<p>Schon mal etwas von <em>Referenzen</em> gehört?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2489342</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2489342</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Fri, 04 Mar 2016 13:41:01 GMT</pubDate></item></channel></rss>