<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Class bloat - &quot;How much is too much?&quot;</title>
		<link>http://www.allegro.cc/forums/view/616494</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 02 Oct 2016 07:41:11 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Background: I&#39;ve been working on/off on a GUI library for years. I don&#39;t program very much anymore, so it&#39;s slow going. I was using std::string for text, but then I read/heard that the std::string class is very bloated. I decided then to switch to <span class="source-code"><a href="http://www.allegro.cc/manual/ALLEGRO_USTR"><span class="a">ALLEGRO_USTR</span></a></span> for all text. While building a string class wrapper for <span class="source-code"><a href="http://www.allegro.cc/manual/ALLEGRO_USTR"><span class="a">ALLEGRO_USTR</span></a></span>, I &quot;borrowed&quot; and modified the string class <a href="https://www.allegro.cc/members/axilmar">axilmar</a>&#39;s <a href="https://github.com/axilmar/ALX">Allegro C++ class wrapper</a>.</p><p>However, I keep coming back to the concept of class bloat. His class is very bloated. I&#39;m now thinking of making each ALLEGRO object into a minimal class.</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="p">#include &lt;allegro/allegro.h&gt;</span>
<span class="number">  2</span><span class="p">#include &lt;memory&gt;</span>
<span class="number">  3</span>
<span class="number">  4</span><span class="k1">template</span> <span class="k3">&lt;</span><span class="k1">class</span> TYPE&gt;
<span class="number">  5</span><span class="k1">static</span> null_deleter<span class="k2">(</span>TYPE <span class="k3">*</span><span class="k2">)</span> <span class="k2">{</span><span class="k2">}</span>
<span class="number">  6</span>
<span class="number">  7</span><span class="k1">template</span> <span class="k3">&lt;</span><span class="k1">class</span> TYPE, <span class="k1">void</span> <span class="k2">(</span>DELETER<span class="k3">*</span><span class="k2">)</span><span class="k2">(</span>T<span class="k3">*</span><span class="k2">)</span><span class="k3">&gt;</span>
<span class="number">  8</span><span class="k1">class</span> SharedObject
<span class="number">  9</span><span class="k2">{</span>
<span class="number"> 10</span>public:
<span class="number"> 11</span>  SharedObject<span class="k2">(</span><span class="k2">)</span> <span class="k2">:</span> m_object<span class="k2">(</span>nullptr<span class="k2">)</span> <span class="k2">{</span><span class="k2">}</span>
<span class="number"> 12</span>
<span class="number"> 13</span>  SharedObject<span class="k2">(</span><span class="k1">const</span> SharedObject <span class="k3">&amp;</span>object<span class="k2">)</span> <span class="k2">:</span> m_object<span class="k2">(</span>object.m_object<span class="k2">)</span> <span class="k2">{</span><span class="k2">}</span>
<span class="number"> 14</span>
<span class="number"> 15</span>  SharedObject<span class="k2">(</span>TYPE <span class="k3">*</span>ptr, <span class="k1">bool</span> managed<span class="k3">=</span> <span class="k1">true</span><span class="k2">)</span> <span class="k2">:</span> m_object<span class="k2">(</span>ptr, <span class="k2">(</span>managed?DELETER:null_deleter<span class="k3">&lt;</span>TYPE&gt;<span class="k2">)</span><span class="k2">)</span> <span class="k2">{</span><span class="k2">}</span>
<span class="number"> 16</span>
<span class="number"> 17</span>  ~SharedObject<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span><span class="k2">}</span>
<span class="number"> 18</span>
<span class="number"> 19</span>protected:
<span class="number"> 20</span>  std::shared_ptr<span class="k3">&lt;</span>TYPE&gt; m_object<span class="k2">;</span>
<span class="number"> 21</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 22</span>
<span class="number"> 23</span>
<span class="number"> 24</span><span class="k1">typedef</span> <span class="k1">typename</span> SharedObject<span class="k3">&lt;</span><a href="http://www.allegro.cc/manual/ALLEGRO_USTR"><span class="a">ALLEGRO_USTR</span></a>, al_ustr_free&gt; AllegroString<span class="k2">;</span>
<span class="number"> 25</span><span class="k1">typedef</span> <span class="k1">typename</span> SharedObject<span class="k3">&lt;</span><a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a>, al_destroy_bitmap&gt; AllegroBitmap<span class="k2">;</span>
</div></div><p>

This removes the bloat from the class, but then I would need &quot;helper&quot; classes to handle object manipulations.</p><p><b>Advice and Comments</b></p><p>1. Keep bloat and make life simpler</p><p>2. Remove bloat and make the code uglier</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// example</span>

<span class="c">// 1</span>
AllegroString str<span class="k2">;</span>

str.assign<span class="k2">(</span><span class="s">"Hello World"</span><span class="k2">)</span><span class="k2">;</span>

<span class="c">//2</span>
AllegroString str<span class="k2">;</span>

StringHelper::assign<span class="k2">(</span>str, <span class="s">"Hello World!"</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (DanielH)</author>
		<pubDate>Wed, 21 Sep 2016 22:59:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/616494/1025156#target">DanielH</a> said:</div><div class="quote"><p>
This removes the bloat from the class, but then I would need &quot;helper&quot; classes to handle object manipulations.
</p></div></div><p>
What&#39;s wrong with that? Although I don&#39;t think we&#39;re thinking of the same approach. Why don&#39;t you just derive concrete classes from your SharedObject class? That&#39;s what I do in my GUI. Everything that I want to be managed derives from EagleObject, and there&#39;s a registry that each object registers itself with. You can delete any object any time, and if you want to delete them all it&#39;s one function call. Simple cleanup, no leftovers. For my widgets, I have a factory that creates all widgets and registers them with the factory. You can give them a group name and delete widgets by group, or singly.</p><div class="source-code snippet"><div class="inner"><pre>
<span class="k1">class</span> SharedObjectBase <span class="k2">{</span>
   <span class="k1">virtual</span> ~SharedObjectBase<span class="k2">(</span><span class="k2">)</span><span class="k2">{</span><span class="k2">}</span>
<span class="k2">}</span><span class="k2">;</span>

<span class="k1">template</span> <span class="k3">&lt;</span><span class="k1">class</span> TYPE, <span class="k1">void</span> <span class="k2">(</span>DELETER<span class="k3">*</span><span class="k2">)</span><span class="k2">(</span>T<span class="k3">*</span><span class="k2">)</span><span class="k3">&gt;</span> 
<span class="k1">class</span> SharedObject <span class="k2">:</span> <span class="k1">public</span> SharedObjectBase <span class="k2">{</span><span class="c">/*...*/</span><span class="k2">}</span><span class="k2">;</span>

<span class="k1">class</span> AllegroString <span class="k2">:</span> <span class="k1">public</span> SharedObject<span class="k3">&lt;</span><a href="http://www.allegro.cc/manual/ALLEGRO_USTR"><span class="a">ALLEGRO_USTR</span></a> , al_free_ustr&gt; <span class="k2">{</span>
   <span class="c">/*...*/</span>
<span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p>

That way you retain the automatic object clean up, but are allowed to expand your class as needed.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Wed, 21 Sep 2016 23:16:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/616494/1025156#target">DanielH</a> said:</div><div class="quote"><p> axilmar&#39;s Allegro C++ class wrapper</p></div></div><p>Oh wow! All his classes are header-only files.  That&#39;s rather clever. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Thu, 22 Sep 2016 07:54:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Bloat is subjective. What is the problem? Is it using up too much space? Does it use too much memory? Is it too slow? Ultimately, you have to pick and choose your battles. Do you want code that performs optimally or do you want code that is powerful and flexible and able to just do what you want without much fuss? You cannot have both. Even with performance you usually have to choose what kinds of performance to optimize for. At the end of the day, you shouldn&#39;t fear &quot;bloat&quot; just because somebody else thinks that&#39;s a problem. You should only care about bloat when it becomes a problem. The C++ standards people are smart people. They aren&#39;t going into the process ignorant and just doing whatever they can come up with. It&#39;s a group of people that have worked on the problem for years and come up with what they think is the best compromise for a standard. Each library and implementation will have its own defined purpose. There can never be a single solution that fits all problems. You need to figure out which solutions best solve your problems. If you don&#39;t know then take the easy route until that doesn&#39;t work anymore and you&#39;re forced to rethink it. Odds are you&#39;ll never reach that point of having to change your mind. But if you do you&#39;ll learn something and next time you&#39;ll know better.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Thu, 22 Sep 2016 08:34:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/616494/1025156#target">DanielH</a> said:</div><div class="quote"><p>it&#39;s slow going.</p></div></div><p>
If you&#39;re re-building base types, this is going to take a while...
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>While building a string class wrapper for ALLEGRO_USTR</p></div></div><p>
I&#39;m not sure if I read that correctly. Does this involve the API of your library ? Does it mean the user who wants to place a label will need to learn how to use your string class first ? What if the user is ALREADY using ALLEGRO_USTR structs ? The user will have to convert each of them to your AllegroString class, pass it to your API, which will unwrap it back to an ALLEGRO_USTR...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Thu, 22 Sep 2016 13:15:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/616494/1025166#target">bamccaig</a> said:</div><div class="quote"><p> Ultimately, you have to pick and choose your battles. Do you want code that performs optimally or do you want code that is powerful and flexible and able to just do what you want without much fuss? You cannot have both.</p></div></div><p>In other words, DanielH, programming is like this:</p><p><span class="remote-thumbnail"><span class="json">{"name":"610565","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/7\/0\/70e4cd81237272ed2c97a6f0560d86ea.gif","w":300,"h":225,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/7\/0\/70e4cd81237272ed2c97a6f0560d86ea"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/7/0/70e4cd81237272ed2c97a6f0560d86ea-240.jpg" alt="610565" width="240" height="180" /></span></p><p>Don&#39;t let it stress you out. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /> KCACO.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Thu, 22 Sep 2016 15:55:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Also, build what you need right now and not what you might need in the future in order to avoid unnecessary &quot;bloat&quot;.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc ( Arvidsson)</author>
		<pubDate>Thu, 22 Sep 2016 17:06:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="https://www.allegro.cc/forums/thread/616494/1025167#target">Audric</a> said:</div><div class="quote"><p>
What if the user is ALREADY using ALLEGRO_USTR structs ? The user will have to convert each of them to your AllegroString class, pass it to your API, which will unwrap it back to an ALLEGRO_USTR...
</p></div></div><p>

Ideally there would be a constructor overload that accepts an ALLEGRO_USTR and just assigns its internal pointer (or maybe copies the structure). It should be very fast and light-weight if the caller is using the same underlying types. It will just suck if the caller isn&#39;t. But that&#39;s always the problem with strings where performance matters (and if performance doesn&#39;t matter then it doesn&#39;t matter that you have to do various conversions anyway).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Thu, 22 Sep 2016 22:00:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I wanted to comment further, but I checked again, and in the ALX wrapper referred, I don&#39;t see any form of &quot;class bloat&quot; as I imagined it : The classes I saw have a constructor, destructor, methods running the allegro functions, and getters/setters for data fields. Nothing more. I can&#39;t imagine it being more sparse, and so I don&#39;t understand what annoyed DanielH, what he wanted to avoid.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Fri, 23 Sep 2016 01:41:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Not annoyed, but curious about other people&#39;s opinions on the subject.</p><p>I like to design my code, for any project, with the intention to share with others. Even if they never do. Personally, I prefer to use std::string for everything because it&#39;s easy to use. I doubt that I would ever have a real need to use Unicode, but this library encompasses Allegro and should use ALLEGRO_USTR for strings.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (DanielH)</author>
		<pubDate>Fri, 23 Sep 2016 03:14:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The need for Unicode comes in when you want to make translations of your program for others to use. std::string and std::wstring don&#39;t really cut it for unicode support. It&#39;s something I have yet to tackle in my GUI. For now I just use std::strings, which I know I will have to replace someday.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 23 Sep 2016 03:20:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I believe that the preferred string class for C++ is CBString from the bstring/bstrlib library. I believe that Unicode was one of their goals, along with performance and bells.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Fri, 23 Sep 2016 05:07:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Use whatever is easiest. Your time is far more valuable than computer time and honestly a GUI is not a real-time operation. If you shave off a few microseconds in your string handling no one could even observe it.</p><p>Maybe I&#39;m just getting too old. Maybe I&#39;ve seen the debate too many times. I remember spending too much time worrying about &quot;bloat&quot;. I worked with another developer who refused to use virtual functions because they were &quot;too bloated.&quot; Now my viewpoint is, I had these same questions back in the 90s when I had a machine with 32Mb of RAM and 100mhz CPU now I have 8192Mb of RAM and 4 3500mhz CPUs. If the worry wasn&#39;t relevant then, it certainly isn&#39;t relevant now, at least for the software we write. Plus, I write code in Java, so I swim in the bloatiest of bloated code, and I&#39;m still doing fine.</p><p>The rules of optimization haven&#39;t changed: Don&#39;t optimize prematurely. Complexity (O(n) vs O(n^2)) blows away any other form of optimization. A quicksort in JS on your phone will destroy your C++ bubble sort on your core i7.</p><p>I do add one caveat. When picking a framework technology, don&#39;t be stupid. This is an exception to the optimize prematurely rule. I did poke fun at JS and C earlier, but using something like JS for example puts limits on the maximum performance you can achieve. So, if you think you&#39;ll get to the point where you must compete on performance, don&#39;t make a choice that forces a rewrite. But, for personal/hobby software, you&#39;ll never run into that. For most enterprise or line-of-business/professional software, even then, you won&#39;t run into that limit. Games and media (codecs) are where it matters. So you&#39;re in the game field, but don&#39;t fool yourself into thinking you have a team of 30 devs to work on figuring out a new way to do strings...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gillius)</author>
		<pubDate>Sat, 24 Sep 2016 06:37:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>^ With that, which is a good post, note that it&#39;s very easy to get distracted from your goal and head off on a useless tangent. That&#39;s fine if it&#39;s just for fun. If it&#39;s professional or you actually aim to make something of your time then you need to learn to recognize the problem and cut it off. It&#39;s rarely practical to do things perfectly. You will likely have to compromise to get things done. You need to decide what matters most and focus on solving that. In the professional world, we try to break problems down into pieces, track them, and prioritize them. That way if you see that some improvement could be made you could make note of it, but keep more important things at the top of the priorities and work on those instead. If things get slow you can always fall back on trying to solve the other problem. But practically, if the gains are minimal or negligible then it probably isn&#39;t worth your time. Computers are cheap compared to manpower. That&#39;s why it&#39;s ridiculous when any employer doesn&#39;t equip their people, especially computer people, with top of the line rigs.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Sat, 24 Sep 2016 09:20:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/616494/1025179#target">DanielH</a> said:</div><div class="quote"><p> Not annoyed, but curious about other people&#39;s opinions on the subject.</p></div></div><p>My stance concerning class bloat changed radically.  I cannot say exactly when, but it was relatively recent.  I used to write, for example, modifier and accessor class members.  Now, I shudder at that sort of code.  I write in C and make structs.  It&#39;s easy.  I feel unburdened.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Gideon Weems)</author>
		<pubDate>Fri, 30 Sep 2016 02:59:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/616494/1025288#target">Gideon Weems</a> said:</div><div class="quote"><p>write in C and make structs. It&#39;s easy. I feel unburdened.</p></div></div><p>

Man, I couldn&#39;t agree more. C is the king!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (princeofspace)</author>
		<pubDate>Fri, 30 Sep 2016 03:57:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>C# supports accessors (i.e., properties) natively. It also supports automatic implementations (i.e., default implementations just returning or setting a standard field). 99.999% of properties in C# are just passthroughs to fields. I really hope the compiler optimizes them away because otherwise it&#39;s ridiculous waste. Dereference the object reference, then dereference the property method, call it, and finally get the value. Instead of dereference the object reference and get the value. And that&#39;s not counting polymorphism overhead...</p><p>Certainly there&#39;s value in just having access to the data and being able to manipulate it... But of course, you also have to accept that your idiot colleague is going to transform the object into an invalid state by mistake and the code is going to crash 3 files away in an unexpected way... There is benefit to accessor methods. If anything, it would be good for the language to support direct access (or as I said, support a rich property syntax, but optimize the default case).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Fri, 30 Sep 2016 06:04:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/616494/1025288#target">Gideon Weems</a> said:</div><div class="quote"><p>
write in C and make structs. It&#39;s easy. I feel unburdened.
</p></div></div><p>
How hard is it to write <span class="source-code"><span class="k1">public</span> <span class="k2">:</span></span>?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 30 Sep 2016 09:30:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/616494/1025293#target">Edgar Reynaldo</a> said:</div><div class="quote"><p>
How hard is it to write public :?
</p></div></div><p>

Not to trifle over semantics here, but in c++ everything inside of a struct is public by default. For instance:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span>
<span class="number">  2</span><span class="k1">class</span> test 
<span class="number">  3</span><span class="k2">{</span>
<span class="number">  4</span>
<span class="number">  5</span>    <span class="k1">int</span> foo<span class="k2">;</span>
<span class="number">  6</span>
<span class="number">  7</span><span class="k2">}</span><span class="k2">;</span>
<span class="number">  8</span>
<span class="number">  9</span><span class="k1">int</span> main<span class="k2">(</span><span class="k1">void</span><span class="k2">)</span>
<span class="number"> 10</span><span class="k2">{</span>
<span class="number"> 11</span>
<span class="number"> 12</span>    test bar<span class="k2">;</span>
<span class="number"> 13</span>    bar.foo<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
<span class="number"> 14</span>    <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 15</span>    
<span class="number"> 16</span><span class="k2">}</span>
</div></div><p>

The compiler will yell at you because &quot;foo&quot; here is private:</p><div class="source-code snippet"><div class="inner"><pre>
.<span class="k3">/</span>main.cpp: In function <span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span><span class="k2">:</span>
.<span class="k3">/</span>main.cpp:15:9: error: <span class="k1">int</span> test::foo is <span class="k1">private</span> within <span class="k1">this</span> context
     bar.foo<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
         ^~~
.<span class="k3">/</span>main.cpp:7:9: note: declared <span class="k1">private</span> here
     <span class="k1">int</span> foo<span class="k2">;</span>
         ^~~
</pre></div></div><p>

Change it from a class to a struct and the error goes away.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (princeofspace)</author>
		<pubDate>Fri, 30 Sep 2016 17:42:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I actually rather like the idea of immutable types as well. It won&#39;t work well for large objects for performance reasons, but for small structs it&#39;s generally just fine. Immutability aids in writing correct code knowing that the object &quot;cannot&quot; change and anything referencing it can rely on that. Need to change the state? Just create a new object!</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="p">#include &lt;iostream&gt;</span>
<span class="number">  2</span>
<span class="number">  3</span><span class="k1">struct</span> Foo
<span class="number">  4</span><span class="k2">{</span>
<span class="number">  5</span>    Foo<span class="k2">(</span><span class="k1">void</span><span class="k2">)</span><span class="k2">:</span> bar_<span class="k2">(</span><span class="n">0</span><span class="k2">)</span>, baz_<span class="k2">(</span><span class="n">0</span><span class="k2">)</span> <span class="k2">{</span><span class="k2">}</span>
<span class="number">  6</span>    Foo<span class="k2">(</span><span class="k1">int</span> r, <span class="k1">int</span> z<span class="k2">)</span><span class="k2">:</span> bar_<span class="k2">(</span>r<span class="k2">)</span>, baz_<span class="k2">(</span>z<span class="k2">)</span> <span class="k2">{</span><span class="k2">}</span>
<span class="number">  7</span>
<span class="number">  8</span>    <span class="k1">int</span> bar<span class="k2">(</span><span class="k1">void</span><span class="k2">)</span> <span class="k1">const</span> <span class="k2">{</span> <span class="k1">return</span> this-&gt;bar_<span class="k2">;</span> <span class="k2">}</span>
<span class="number">  9</span>    <span class="k1">int</span> baz<span class="k2">(</span><span class="k1">void</span><span class="k2">)</span> <span class="k1">const</span> <span class="k2">{</span> <span class="k1">return</span> this-&gt;baz_<span class="k2">;</span> <span class="k2">}</span>
<span class="number"> 10</span>
<span class="number"> 11</span>    Foo frobinate<span class="k2">(</span><span class="k1">int</span> r, <span class="k1">int</span> z<span class="k2">)</span>
<span class="number"> 12</span>    <span class="k2">{</span>
<span class="number"> 13</span>        <span class="k1">return</span> Foo<span class="k2">(</span>this-&gt;bar_ <span class="k3">+</span> r,
<span class="number"> 14</span>                   this-&gt;baz_ <span class="k3">*</span> z<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 15</span>    <span class="k2">}</span>
<span class="number"> 16</span>
<span class="number"> 17</span>    Foo frobinate<span class="k2">(</span><span class="k1">const</span> Foo <span class="k3">&amp;</span> f<span class="k2">)</span>
<span class="number"> 18</span>    <span class="k2">{</span>
<span class="number"> 19</span>        <span class="k1">return</span> this-&gt;frobinate<span class="k2">(</span>f.bar<span class="k2">(</span><span class="k2">)</span>, f.baz<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 20</span>    <span class="k2">}</span>
<span class="number"> 21</span>
<span class="number"> 22</span>protected:
<span class="number"> 23</span>    <span class="k1">int</span> bar_<span class="k2">;</span>
<span class="number"> 24</span>    <span class="k1">int</span> baz_<span class="k2">;</span>
<span class="number"> 25</span><span class="k2">}</span><span class="k2">;</span>
<span class="number"> 26</span>
<span class="number"> 27</span><span class="k1">int</span> main<span class="k2">(</span><span class="k1">int</span> argc, <span class="k1">char</span><span class="k3">*</span> argv <span class="k2">[</span><span class="k2">]</span><span class="k2">)</span>
<span class="number"> 28</span><span class="k2">{</span>
<span class="number"> 29</span>    Foo f<span class="k2">;</span>
<span class="number"> 30</span>    Foo g<span class="k2">(</span><span class="n">5</span>, <span class="n">10</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 31</span>    Foo h <span class="k3">=</span> f.frobinate<span class="k2">(</span>g<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 32</span>
<span class="number"> 33</span>    std::cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"{'bar':'"</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> h.bar<span class="k2">(</span><span class="k2">)</span>
<span class="number"> 34</span>              <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"','baz':'"</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> h.baz<span class="k2">(</span><span class="k2">)</span>
<span class="number"> 35</span>              <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"'}"</span> <span class="k3">&lt;</span><span class="k3">&lt;</span> std::endl<span class="k2">;</span>
<span class="number"> 36</span>
<span class="number"> 37</span>    <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 38</span><span class="k2">}</span>
</div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Fri, 30 Sep 2016 23:06:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/616494/1025298#target">princeofspace</a> said:</div><div class="quote"><p>
Not to trifle over semantics here, but in c++ everything inside of a struct is public by default. For instance:
</p></div></div><p>
Not to trifle over encapsulation, but not everything <b>should</b> be public. In C you have to accomplish that with hidden headers and opaque structs. C++ does it naturally. :/</p><div class="quote_container"><div class="title">princeofspace said:</div><div class="quote"><p>
Change it from a class to a struct and the error goes away. 
</p></div></div><p>
The only error there was not using the public label. structs shouldn&#39;t even exist inside C++, they&#39;re only there to hold the hands of C programmers who can&#39;t grasp encapsulation.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sat, 01 Oct 2016 00:41:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I agree that <span class="source-code">structs</span> are nice.  There is some epiphanic purity to them.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Sat, 01 Oct 2016 07:36:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Busted:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">struct</span> Date
<span class="number">  2</span><span class="k2">{</span>
<span class="number">  3</span>    <span class="k1">int</span> year<span class="k2">;</span>
<span class="number">  4</span>    <span class="k1">int</span> month<span class="k2">;</span>
<span class="number">  5</span>    <span class="k1">int</span> day<span class="k2">;</span>
<span class="number">  6</span><span class="k2">}</span><span class="k2">;</span>
<span class="number">  7</span>
<span class="number">  8</span>Date foo<span class="k2">(</span>Date d<span class="k2">)</span>
<span class="number">  9</span><span class="k2">{</span>
<span class="number"> 10</span>    <span class="c">// Oh shit...</span>
<span class="number"> 11</span><span class="k2">}</span>
<span class="number"> 12</span>
<span class="number"> 13</span><span class="k1">int</span> main<span class="k2">(</span><span class="k1">int</span> argc, <span class="k1">char</span> <span class="k3">*</span> argv<span class="k2">[</span><span class="k2">]</span><span class="k2">)</span>
<span class="number"> 14</span><span class="k2">{</span>
<span class="number"> 15</span>    Date d <span class="k3">=</span> <span class="k2">{</span> <span class="n">2016</span>, <span class="n">13</span>, <span class="n">45</span> <span class="k2">}</span><span class="k2">;</span>
<span class="number"> 16</span>    Date d2 <span class="k3">=</span> foo<span class="k2">(</span>d<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 17</span>
<span class="number"> 18</span>    <span class="c">// WTF is d2...?</span>
<span class="number"> 19</span>
<span class="number"> 20</span>    <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 21</span><span class="k2">}</span>
</div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Sat, 01 Oct 2016 09:11:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>What&#39;s up with your code bam?  You should be getting a warning when you try to compile:
</p><div class="source-code snippet"><div class="inner"><pre>struct.cpp:11:1: warning: control reaches end of non-void function <span class="k2">[</span><span class="k3">-</span>Wreturn-type<span class="k2">]</span>
<span class="k2">}</span>
^
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Sat, 01 Oct 2016 18:04:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/616494/1025303#target">Edgar Reynaldo</a> said:</div><div class="quote"><p>
structs shouldn&#39;t even exist inside C++, they&#39;re only there to hold the hands of C programmers who can&#39;t grasp encapsulation.
</p></div></div><p>

Lots of languages bend over backwards to be compatible with C, and C++ is perhaps the strongest example. Because C has no (built-in) concept of classes, POD structs are necessary.</p><p>Back on topic: I used to be concerned about this sort of thing until I read about how <a href="http://www.catb.org/esr/structure-packing/">struct padding</a> works. In other words, the computer will &quot;bloat&quot; a struct by a few bytes here and there, to more naturally align members according to your architecture. Sprites data (before media assets) in my game is no more than 256 bytes, <i>more</i> than enough for my needs.</p><p>As others here have commented, even older hardware can readily handle a largish class.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (princeofspace)</author>
		<pubDate>Sat, 01 Oct 2016 19:41:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="https://www.allegro.cc/forums/thread/616494/1025329#target">Mark Oates</a> said:</div><div class="quote"><p>
What&#39;s up with your code bam?  You should be getting a warning when you try to compile:</p><div class="source-code snippet"><div class="inner"><pre>struct.cpp:11:1: warning: control reaches end of non-void function <span class="k2">[</span><span class="k3">-</span>Wreturn-type<span class="k2">]</span>
<span class="k2">}</span>
^
</pre></div></div><p>
</p></div></div><p>

I figured it was obvious that the function was undefined, but assumed the date was sensible... I don&#39;t even remember if I compiled that (probably not?).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Sun, 02 Oct 2016 07:41:11 +0000</pubDate>
	</item>
</rss>
