<?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[Designfrage: Werte erst berechnen wenn notwendig]]></title><description><![CDATA[<p>Angenommen ich habe folgende Klasse:</p>
<pre><code>template&lt;class T&gt;
		class Statistics {
		public:
			// Kann die Reihenfolge u.U. ändern (z.B. bei median)
			Statistics(std::vector&lt;T&gt;&amp; vecData) : m_refVecData(vecData) {

          }

private:
       std::vector&lt;T&gt;&amp; m_refVecData;
};
</code></pre>
<p>Jetzt bietet die Klasse verschiedene Funktionen an:</p>
<pre><code>inline double sum() const {
				return std::accumulate(m_refVecData.begin(), m_refVecData.end(), 0.0);
			}
</code></pre>
<p>Die Summe soll also erst berechnet werden, wenn sie auch wirklich gebraucht wird. Hier schon das erste Problem. Wenn jemand die Funktion zweimal aufruft, dann wird sie auch zweimal berechnet. Soll ich die Summe jetzt als member-variable speichern und dazu einen boolean, ob sie bereits berechnet wurde?</p>
<p>Das gleiche dann bei anderen Funktionen, die stellenweise auch voneinander abhängen:</p>
<pre><code>inline double mean() const {
				return sum() / m_refVecData.size();
			}
</code></pre>
<p>Wie würdet ihr das lösen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/334282/designfrage-werte-erst-berechnen-wenn-notwendig</link><generator>RSS for Node</generator><lastBuildDate>Sat, 25 Apr 2026 20:36:22 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/334282.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 03 Sep 2015 07:34:17 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Designfrage: Werte erst berechnen wenn notwendig on Thu, 03 Sep 2015 07:34:17 GMT]]></title><description><![CDATA[<p>Angenommen ich habe folgende Klasse:</p>
<pre><code>template&lt;class T&gt;
		class Statistics {
		public:
			// Kann die Reihenfolge u.U. ändern (z.B. bei median)
			Statistics(std::vector&lt;T&gt;&amp; vecData) : m_refVecData(vecData) {

          }

private:
       std::vector&lt;T&gt;&amp; m_refVecData;
};
</code></pre>
<p>Jetzt bietet die Klasse verschiedene Funktionen an:</p>
<pre><code>inline double sum() const {
				return std::accumulate(m_refVecData.begin(), m_refVecData.end(), 0.0);
			}
</code></pre>
<p>Die Summe soll also erst berechnet werden, wenn sie auch wirklich gebraucht wird. Hier schon das erste Problem. Wenn jemand die Funktion zweimal aufruft, dann wird sie auch zweimal berechnet. Soll ich die Summe jetzt als member-variable speichern und dazu einen boolean, ob sie bereits berechnet wurde?</p>
<p>Das gleiche dann bei anderen Funktionen, die stellenweise auch voneinander abhängen:</p>
<pre><code>inline double mean() const {
				return sum() / m_refVecData.size();
			}
</code></pre>
<p>Wie würdet ihr das lösen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2466669</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2466669</guid><dc:creator><![CDATA[Designer32]]></dc:creator><pubDate>Thu, 03 Sep 2015 07:34:17 GMT</pubDate></item><item><title><![CDATA[Reply to Designfrage: Werte erst berechnen wenn notwendig on Thu, 03 Sep 2015 07:41:56 GMT]]></title><description><![CDATA[<p>Vlt. gar keine Klasse und alles in einzelne Template-Funktionen? Das kam mir noch in den Sinn <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2466671</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2466671</guid><dc:creator><![CDATA[Designer32]]></dc:creator><pubDate>Thu, 03 Sep 2015 07:41:56 GMT</pubDate></item><item><title><![CDATA[Reply to Designfrage: Werte erst berechnen wenn notwendig on Thu, 03 Sep 2015 10:29:11 GMT]]></title><description><![CDATA[<p>Designer32 schrieb:</p>
<blockquote>
<p>Soll ich die Summe jetzt als member-variable speichern und dazu einen boolean, ob sie bereits berechnet wurde?</p>
</blockquote>
<p>Das kann man durchausmachen, wenn Geschwindigkeit wichtiger als Speicherplatz ist. Aber nicht vergessen den boolean zu setzen, sobald sich Werte im Array aendern.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2466709</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2466709</guid><dc:creator><![CDATA[TGGC]]></dc:creator><pubDate>Thu, 03 Sep 2015 10:29:11 GMT</pubDate></item><item><title><![CDATA[Reply to Designfrage: Werte erst berechnen wenn notwendig on Thu, 03 Sep 2015 14:48:47 GMT]]></title><description><![CDATA[<p>Jupp, eine freie Funktion ist hier sinnvoller:</p>
<pre><code class="language-cpp">double sum(std::vector&lt;double&gt; const &amp; vecData) {
    return std::accumulate(vecData.begin(), vecData.end(), 0.0);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2466737</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2466737</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Thu, 03 Sep 2015 14:48:47 GMT</pubDate></item><item><title><![CDATA[Reply to Designfrage: Werte erst berechnen wenn notwendig on Thu, 03 Sep 2015 15:33:33 GMT]]></title><description><![CDATA[<p>Wenn schon denn schon <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
<pre><code>template &lt;typename Range&gt;
constexpr auto sum(Range const&amp; range) {
    using std::begin; using std::end;
    return std::accumulate(begin(range), end(range), typename std::iterator_traits&lt;decltype(begin(range))&gt;::value_type{});
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2466741</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2466741</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Thu, 03 Sep 2015 15:33:33 GMT</pubDate></item><item><title><![CDATA[Reply to Designfrage: Werte erst berechnen wenn notwendig on Thu, 03 Sep 2015 16:47:23 GMT]]></title><description><![CDATA[<p>Siehe auch hier: <a href="http://gameprogrammingpatterns.com/dirty-flag.html" rel="nofollow">http://gameprogrammingpatterns.com/dirty-flag.html</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2466751</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2466751</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Thu, 03 Sep 2015 16:47:23 GMT</pubDate></item><item><title><![CDATA[Reply to Designfrage: Werte erst berechnen wenn notwendig on Mon, 07 Sep 2015 10:01:32 GMT]]></title><description><![CDATA[<p>Lazy initialization! In deinem Fall halt Lazy-Sum. <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/2467036</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2467036</guid><dc:creator><![CDATA[Artchi]]></dc:creator><pubDate>Mon, 07 Sep 2015 10:01:32 GMT</pubDate></item></channel></rss>