<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Slacy's Blog &#187; c++</title>
	<atom:link href="http://slacy.com/blog/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://slacy.com/blog</link>
	<description>This site is solar powered!</description>
	<lastBuildDate>Fri, 27 Jan 2012 04:33:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Making a copy of constructor arguments in C++</title>
		<link>http://slacy.com/blog/2010/05/making-a-copy-of-constructor-arguments-in-c/</link>
		<comments>http://slacy.com/blog/2010/05/making-a-copy-of-constructor-arguments-in-c/#comments</comments>
		<pubDate>Thu, 20 May 2010 21:12:36 +0000</pubDate>
		<dc:creator>slacy</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[pointers]]></category>
		<category><![CDATA[python interface]]></category>
		<category><![CDATA[refcounting]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://slacy.com/blog/?p=1189</guid>
		<description><![CDATA[Okay, maybe my C++ is getting rusty, but I&#8217;m stammering at coming up with a reasonable solution to this problem: Imagine a class, like this one: class Foo { public: Foo(string &#38;name) : name_(name.c_str()) { } private: char *name_; } &#8230; <a href="http://slacy.com/blog/2010/05/making-a-copy-of-constructor-arguments-in-c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Okay, maybe my C++ is getting rusty, but I&#8217;m stammering at coming up with a reasonable solution to this problem:</p>
<p>Imagine a class, like this one:</p>
<pre>class Foo {
  public:
    Foo(string &amp;name) : name_(name.c_str()) { }
  private:
    char *name_;
}</pre>
<p>As you can see, it takes the guts of the string argument passed to the constructor, and stores them internally.  In other words, it assumes that the caller is going to manage the lifetime of the string argument so that it lives at least as long as the instance of Foo.    Ignore that this might be horribly bad style for a second, and pretend that this is an external class that you want to interface with.  What I want to be able to do is this:</p>
<pre>Foo f = Foo(string("foo"));</pre>
<p>But, of course, that will totally crash.  So, I&#8217;d like to write a wrapper class, that looks like this:</p>
<pre>class FooWrapper : public Foo {
  public:
    FooWrapper(string &amp;name) :
        Foo(make_a_copy(name, name_copy_)) { } 

    string &amp;make_a_copy(string &amp;src, string &amp;dest) {
        dest = src;
        return dest;
    }

  private:
    string name_copy_;
}</pre>
<p>But here&#8217;s the problem:  The code in make_a_copy() crashes because the guts of the string representation stored in name_copy_ is total garbage and not initialized to a reasonable state yet.  I could create a factory function and add some yucky methods to FooWrapper, but I&#8217;d really rather not.  Is there an easier way to accomplish this &#8220;copy on construct&#8221; behavior?</p>
]]></content:encoded>
			<wfw:commentRss>http://slacy.com/blog/2010/05/making-a-copy-of-constructor-arguments-in-c/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
	</item>
		<item>
		<title>Subject Oriented Programming (SOP) vs. Object Oriented Programming (OOP)</title>
		<link>http://slacy.com/blog/2010/03/suject-oriented-programming-sop-vs-object-oriented-programming-oop/</link>
		<comments>http://slacy.com/blog/2010/03/suject-oriented-programming-sop-vs-object-oriented-programming-oop/#comments</comments>
		<pubDate>Mon, 22 Mar 2010 23:03:13 +0000</pubDate>
		<dc:creator>slacy</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[direct object]]></category>
		<category><![CDATA[indirect object]]></category>
		<category><![CDATA[object-oriented]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[subject-oriented]]></category>

		<guid isPermaLink="false">http://slacy.com/blog/?p=1141</guid>
		<description><![CDATA[When I&#8217;m programming, I usually anthroporporphize the code and the actors involved, and that means when I&#8217;m thinking about program flow, I think of it as if they&#8217;re real actors doing the tasks needed, and then I convert that back &#8230; <a href="http://slacy.com/blog/2010/03/suject-oriented-programming-sop-vs-object-oriented-programming-oop/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When I&#8217;m programming, I usually anthroporporphize the code and the actors involved, and that means when I&#8217;m thinking about program flow, I think of it as if they&#8217;re real actors doing the tasks needed, and then I convert that back into code.  But, when reading other people&#8217;s code, I often times find it hard to convert back into my internal actor-based representation.  I realized that this is because the term &#8220;Object Oriented Programming&#8221; should likely be called &#8220;Subject Oriented Programming&#8221;  I&#8217;m using the term Subject the way you use it when describing English sentence structure.  Take a look at this:</p>
<p>&#8220;The boy put the box in the bag.&#8221;</p>
<p>Just to refresh your grade school English (which I had to do as well), take a brief look at the <a href="http://en.wikipedia.org/wiki/Sentence_diagram">sentence diagramming article on wikipedia</a>.  Our sentence above boils down to:</p>
<p>Subject: &#8220;The boy&#8221;<br />
Predicate &amp; Direct object: &#8220;put the box&#8221;<br />
Indirect object: &#8220;in the bag&#8221;</p>
<p>How would you represent that in code? Think about it for a second before continuing.  So, here are the possibilities of how this could be written with a single method call:</p>
<pre>// Subject-Oriented programming:
// "The boy put the box in the bag"
boy.PutIn(box, bag);

// Direct-object oriented programming:
// "The box was put in the bag by the boy"
box.PutIn(bag, boy);

// Indirect-object oriented programming:
// "The bag, to which was added a box, was done by the boy."  OR
// "A bag now has a box that was put there by the boy."
bag.AddTo(box, boy);</pre>
<p>So, which is correct via the principles of OOP?  What&#8217;s the correct place for the method that does the work?  Is it on the Subject of the sentence?  The Direct Object?  Were you taught how to design this?  (i.e. in school?)</p>
<p>In practice, method placement is one of the biggest factors in how maintainable &amp; testable your code is, and code is rarely as simple as what I&#8217;ve shown above.  How do you figure out where the right place for your methods are?</p>
<p>One code base where this was thought about a lot was in Python&#8217;s handling of string &amp; list methods.   The split() and join() methods in Python are both on the string type.  In other words:  The list type is rigid in that its methods contain no string-manipulation.  This seems good at first until you read the code:</p>
<pre>s = "Hello, world"
# Split the string into a list of "Hello" and "world":
l = s.split(',')
# Now, take that resulting list, and join it back together:
j = ','.join(l)</pre>
<p>Wow, that last line looks weird!  What&#8217;s going on there?  We&#8217;re creating a temporary string &#8220;,&#8221; and then calling the join() method!  Weird!  For that last line, I would have much preferred to write:</p>
<pre>j = l.join(',')</pre>
<p>To be symmetric with the original splitting code, but to write things that way would place a string generating method &#8220;join()&#8221; on the list class, which also seems wrong.</p>
]]></content:encoded>
			<wfw:commentRss>http://slacy.com/blog/2010/03/suject-oriented-programming-sop-vs-object-oriented-programming-oop/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
	</item>
		<item>
		<title>How do you make change?</title>
		<link>http://slacy.com/blog/2008/01/how-do-you-make-change/</link>
		<comments>http://slacy.com/blog/2008/01/how-do-you-make-change/#comments</comments>
		<pubDate>Wed, 30 Jan 2008 23:31:43 +0000</pubDate>
		<dc:creator>slacy</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[coinage]]></category>
		<category><![CDATA[denominations]]></category>

		<guid isPermaLink="false">http://slacy.com/blog/index.php/2008/01/30/how-do-you-make-change/</guid>
		<description><![CDATA[I&#8217;ve always been fascinated by the denominations of money, and specifically, which coin values different countries use. Here in the US, we use the set of values {1,5,10,25}, and I&#8217;ve often wondered whether this is the best set of values. &#8230; <a href="http://slacy.com/blog/2008/01/how-do-you-make-change/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve always been fascinated by the denominations of money, and specifically, which coin values different countries use.  Here in the US, we use the set of values {1,5,10,25}, and I&#8217;ve often wondered whether this is the best set of values.  The question is:  How do you figure out what &#8216;best&#8217; is?  </p>
<p>Previously, I wrote a program that, given a set like {1,5,10,25}, counted the number of coins needed to make every possible value between 1 and 99 cents.  I thought that number would be a pretty good indication of how good the set of values was.  The best set for 4 coins was a tie between {1, 5, 18, 29} and {1, 5, 18, 25}.  </p>
<p>As I discussed this solution with my friends (long ago) they all mentioned that this wasn&#8217;t a very realistic or good measure of how well a set of values performs in the real world.  It was suggested that a better metric would be to model real-world transactions, and the number of coins needed.</p>
<p>So, I set off to write a new program.  The algorithm would model a single buyer and a number of &#8216;purchases&#8217; between 0 and 99 cents.  Then, I could calculate the average number of coins in the virtual &#8216;coin pocket&#8217;.  This average pocket size would be my measure for the &#8216;best&#8217; set of coin values.  The pocket would contain an infinite number of dollar bills, so it never runs out of money, and the bills aren&#8217;t counted as &#8216;coinage&#8217;. </p>
<p>But, an interesting question came into play:  When making a purchase, the program needs to decide how much to pay for the item, given the contents of the pocket.  I started off by implementing a fairly naive algorithm that ended up preferring to use large coins.  After looking at the results, I found that the pocket was accumulating as many as 18 pennies before spending them.   Although this might be how I personally work, it didn&#8217;t seem to be how most people would do it, and it didn&#8217;t seem like a good model for the program.  So, I pose the question to you, the reader!</p>
<p>If you&#8217;ve got 4 pennies and a bunch of dollar bills, do you usually put down pennies to &#8217;round up&#8217; your change?  For example, would you pay $1.03 for an item that cost $0.38? </p>
<p>Would you put down extra nickels and dimes to &#8217;round up&#8217; to dimes and quarters? How often?  For example, would you pay $1.08 or for an item that cost $0.38 to get back $0.70?  This case is particularly interesting because you give up 4 coins (3 pennies and a nickel) to get back 4 higher valued coins (2 quarters and 2 dimes).   There&#8217;s no reduction in the coin count, but it &#8216;feels right&#8217; to get higher valued coins back. </p>
<p>How about $1.13 to pay for $0.38?  In this case, you would get back exactly $0.75.  What if the $0.13 was made up of 13 pennies? Would you still do it? </p>
<p>How about paying $1.63 for $0.88 to get back exactly $0.75?  What if the $1.63 were made up of an odd assortment of coins, like 5 quarters, 2 dimes, 2 nickels and 8 pennies?  Would you still do it?</p>
<p>At some point (like in the previous example) we start to trade off our own time needed to add and compose our purchase amount for the convenience of just ending the transaction.  I think we also consider the cashier&#8217;s time and patience, since they may not tolerate waiting as we ponder and compose the &#8216;perfect&#8217; amount.  Not to mention the other people in line behind us!</p>
<p>I think the solution here is going to be a rule-based system, with things like:</p>
<p>If you have enough pennies to round up to the nearest $0.05, then do it.<br />
If you have enough dimes &#038; nickels to round up to the nearest $0.25, then do it.<br />
Don&#8217;t overpay for the item by more than $0.50.<br />
Don&#8217;t give the cashier more than N coins.  (whats N?)</p>
<p>I&#8217;ve started to code up some of this stuff in C++, but I&#8217;m kind of stymied by trying to come up with a good generalized solution to this problem, especially since the rules conflict and overlap.  (And note that any solution has to work for arbitrary values of coins, like {1,4,12,24}) </p>
<p>How would you go about tackling this problem?</p>
]]></content:encoded>
			<wfw:commentRss>http://slacy.com/blog/2008/01/how-do-you-make-change/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
	</item>
		<item>
		<title>Is there a space after the *, or after the &amp;?</title>
		<link>http://slacy.com/blog/2008/01/is-there-a-space-after-the-or-after-the/</link>
		<comments>http://slacy.com/blog/2008/01/is-there-a-space-after-the-or-after-the/#comments</comments>
		<pubDate>Sat, 12 Jan 2008 04:40:27 +0000</pubDate>
		<dc:creator>slacy</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[declarations]]></category>
		<category><![CDATA[pointers]]></category>
		<category><![CDATA[style]]></category>
		<category><![CDATA[types]]></category>

		<guid isPermaLink="false">http://slacy.com/blog/index.php/2008/01/11/is-there-a-space-after-the-or-after-the/</guid>
		<description><![CDATA[No, there is not a space after the *. Take a look at this example: value = *pointer; Note how there&#8217;s not a space after the asterisk. Also note how the type of &#8220;value&#8221; and the type of &#8220;*pointer&#8221; are &#8230; <a href="http://slacy.com/blog/2008/01/is-there-a-space-after-the-or-after-the/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>No, there is not a space after the *.  Take a look at this example:</p>
<pre>
value = *pointer;
</pre>
<p>Note how there&#8217;s not a space after the asterisk.   Also note how the type of &#8220;value&#8221; and the type of &#8220;*pointer&#8221; are the same. </p>
<p>Thus, when you declare these variables, you should do it like this:</p>
<pre>
Type value;
Type *pointer;

value = *pointer;
</pre>
<p>Why would you ever want to put the asterisk next to the type name?  Why would that <strong>ever</strong> make sense?  I just don&#8217;t get it!</p>
]]></content:encoded>
			<wfw:commentRss>http://slacy.com/blog/2008/01/is-there-a-space-after-the-or-after-the/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
	</item>
	</channel>
</rss>

