<?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[static getter&#x2F;setter]]></title><description><![CDATA[<p dir="auto">i need to have a static member in my class that will be assigned a value a few times during the program. i have a few ideas on how to do this but i'm not sure which is more correct. here's those ways:</p>
<ol>
<li>have a static member, and have a static method that'll return a reference to the static member</li>
<li>have a static method that will create a static variable inside it and return a reference to it</li>
</ol>
<p dir="auto">so which is more correct? or there's a better way? don't forget that i need to assign value to this. the reason i'm not sure which one to use is that if i use the 2nd option, is it appropriate to assign a value to function return value?</p>
<p dir="auto">thanks</p>
]]></description><link>https://forum.qt.io/topic/83883/static-getter-setter</link><generator>RSS for Node</generator><lastBuildDate>Sun, 15 Nov 2026 15:59:07 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/83883.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 05 Oct 2017 06:00:41 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to static getter&#x2F;setter on Thu, 05 Oct 2017 08:44:20 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/user4592357">@<bdi>user4592357</bdi></a> said in <a href="/post/418904">static getter/setter</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/aha_1980">@<bdi>aha_1980</bdi></a><br />
good idea. but i don't need to do checks before assigning, should i still use static getter + static setter?</p>
</blockquote>
<p dir="auto">Isn't the reason to use classes that you can hide your data? And even if you don't need a check <em>now</em>, this can change in half a year. Further, it avoids the problems <a class="plugin-mentions-user plugin-mentions-a" href="/user/johansolo">@<bdi>JohanSolo</bdi></a> mentioned.</p>
<blockquote>
<p dir="auto">it'll be a QString, so a reference or value?</p>
</blockquote>
<p dir="auto">In this case I'd use <code>void mySetter(const QString &amp;str);</code> like all the Qt API do it.</p>
]]></description><link>https://forum.qt.io/post/418908</link><guid isPermaLink="true">https://forum.qt.io/post/418908</guid><dc:creator><![CDATA[aha_1980]]></dc:creator><pubDate>Thu, 05 Oct 2017 08:44:20 GMT</pubDate></item><item><title><![CDATA[Reply to static getter&#x2F;setter on Thu, 05 Oct 2017 07:56:09 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/aha_1980">@<bdi>aha_1980</bdi></a></p>
<p dir="auto">good idea. but i don't need to do checks before assigning, should i still use static getter + static setter?</p>
<p dir="auto">it'll be a QString, so a reference or value?</p>
]]></description><link>https://forum.qt.io/post/418904</link><guid isPermaLink="true">https://forum.qt.io/post/418904</guid><dc:creator><![CDATA[user4592357]]></dc:creator><pubDate>Thu, 05 Oct 2017 07:56:09 GMT</pubDate></item><item><title><![CDATA[Reply to static getter&#x2F;setter on Thu, 05 Oct 2017 06:56:23 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/user4592357">@<bdi>user4592357</bdi></a> said in <a href="/post/418876">static getter/setter</a>:</p>
<blockquote>
<p dir="auto">have a static method that will create a static variable inside it and return a reference to it</p>
</blockquote>
<p dir="auto">Even if this is possible, I would not recomment to do this. The only place I have seen a pattern like this is for singleton, a la:</p>
<pre><code>static *Singleton::instance()
{
  static Singleton *s = new Singleton;
  return s;
}
</code></pre>
<p dir="auto">And what about option three: Having a static setter also? Therefore you encapsulate the access to your variable and can do sanity checks before the assignment also.</p>
<p dir="auto">And as you mentioned reference: if the size of your variable is &lt;= 16 bytes, it may be more efficient to pass by value. If it is larger, pass by const reference.</p>
]]></description><link>https://forum.qt.io/post/418887</link><guid isPermaLink="true">https://forum.qt.io/post/418887</guid><dc:creator><![CDATA[aha_1980]]></dc:creator><pubDate>Thu, 05 Oct 2017 06:56:23 GMT</pubDate></item><item><title><![CDATA[Reply to static getter&#x2F;setter on Thu, 05 Oct 2017 06:23:48 GMT]]></title><description><![CDATA[<p dir="auto">IMHO solution 1 is better: you have a static member and static getter / setter. If you go with the second solution, then you're forced to use the same accessor for get / set, which might cause problem if you have a typo like <code>if ( MyClass::myStaticGetter() = 8 )</code> instead of <code>if ( MyClass::myStaticGetter() == 8 )</code>.</p>
]]></description><link>https://forum.qt.io/post/418881</link><guid isPermaLink="true">https://forum.qt.io/post/418881</guid><dc:creator><![CDATA[JohanSolo]]></dc:creator><pubDate>Thu, 05 Oct 2017 06:23:48 GMT</pubDate></item></channel></rss>