<?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/"
	>

<channel>
	<title>Modern C++</title>
	<atom:link href="http://www.markturney.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markturney.com/blog</link>
	<description>tips, tricks, and realizations</description>
	<lastBuildDate>Sun, 15 Apr 2012 17:55:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>++x vs. x++ and saying what you mean</title>
		<link>http://www.markturney.com/blog/2011/10/x-vs-x-and-saying-what-you-mean/</link>
		<comments>http://www.markturney.com/blog/2011/10/x-vs-x-and-saying-what-you-mean/#comments</comments>
		<pubDate>Mon, 03 Oct 2011 06:20:30 +0000</pubDate>
		<dc:creator>Mark Turney</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[post-increment]]></category>
		<category><![CDATA[postfix]]></category>
		<category><![CDATA[pre-increment]]></category>
		<category><![CDATA[prefix]]></category>

		<guid isPermaLink="false">http://www.markturney.com/blog/?p=20</guid>
		<description><![CDATA[What&#8217;s the same Whether we pre-increment (++x) or post-increment (x++), we are adding one to the value of the variable (x += 1). int x = 5; ++x; std::cout &#60;&#60; x &#60;&#60; std::endl; // prints "6" x++; std::cout &#60;&#60; x &#8230; <a href="http://www.markturney.com/blog/2011/10/x-vs-x-and-saying-what-you-mean/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><strong>What&#8217;s the same</strong></p>
<p>Whether we pre-increment (++x) or post-increment (x++), we are adding one to the value of the variable (x += 1).</p>
<pre class="brush: cpp;">int x = 5;
++x;
std::cout &lt;&lt; x &lt;&lt; std::endl;  // prints "6"
x++;
std::cout &lt;&lt; x &lt;&lt; std::endl;  // prints "7"</pre>
<p><strong>What&#8217;s different</strong></p>
<p>The only difference between the two operations is the return type.</p>
<p><strong>Function Signatures</strong></p>
<p>As the function signature for pre-increment (++x) reveals, it adds one to the supplied object and returns a reference to this modified object.</p>
<pre class="brush: cpp;">// Function signature for pre-increment operator in custom class.
Example &amp; operator++();</pre>
<p>In contrast, the function signature for post-increment (x++) returns by value.  It copies the value before modification, adds one to the supplied object, and returns the original copied value as an unnamed temporary object.</p>
<pre class="brush: cpp;">// Function signature for post-increment operator in custom class.
//  The dummy int argument only denotes post-increment.
Example operator++(int);</pre>
<p><strong>Your intent decides which operation you should use</strong></p>
<ul>
<li>If you don&#8217;t care about the return type because you aren&#8217;t capturing it, you should stick to the prefix form (++x) because it doesn&#8217;t waste time creating a temporary object that you won&#8217;t use.</li>
<li>If you want to increment and immediately use the object you should use pre-increment (++x).</li>
<li> If you want to increment an object but retrieve a copy of it&#8217;s value before it was modified you should use post-increment (x++).</li>
</ul>
<p><strong>Conclusion</strong></p>
<p>Stick to pre-increment (++x) in almost all situations with the rare exception being when you want a copy of the variable before it was incremented.</p>
<p><strong>Examples</strong></p>
<pre class="brush: cpp;"">int a = 5;
int b = 5;

int c = a++;  // c == 5
int d = ++b;  // d == 6

int &amp; e = ++a; // e now references a which has been incremented to 7
int &amp; f = b++; // compile time error - non const reference to temporary</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.markturney.com/blog/2011/10/x-vs-x-and-saying-what-you-mean/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

