<?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>Coding Friends &#187; const</title>
	<atom:link href="http://www.codingfriends.com/index.php/tag/const/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codingfriends.com</link>
	<description>Coding Friends, place for developers.</description>
	<lastBuildDate>Sun, 04 Dec 2011 21:11:08 +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>const &#8211; constants in functions</title>
		<link>http://www.codingfriends.com/index.php/2010/01/29/const-constants-in-functions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=const-constants-in-functions</link>
		<comments>http://www.codingfriends.com/index.php/2010/01/29/const-constants-in-functions/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 10:43:44 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[const]]></category>
		<category><![CDATA[functions]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=599</guid>
		<description><![CDATA[Const is a constant type, it makes what is talking to a constant value. The value cannot be altered in anyway, a good reason for this could be for a constant string for error codes or if the value of a variable needs to be checked but not altered in anyway. Pointers can be constants [...]]]></description>
			<content:encoded><![CDATA[<p>Const is a constant type, it makes what is talking to a constant value.  The value cannot be altered in anyway, a good reason for this could be for a constant string for error codes or if the value of a variable needs to be checked but not altered in anyway.</p>
<p>Pointers can be constants too and also there are many places that you can put the const type.  Here are some examples</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// constant integer value</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> constValue <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span><span style="color: #008080;">;</span>
<span style="color: #666666;">// normal value.</span>
<span style="color: #0000ff;">int</span> Value <span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">//pointers can be constants too</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span> pConst <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>constValue<span style="color: #008080;">;</span>
<span style="color: #666666;">//the pConst is a pointer to a const int value, the value pointed to cannot be altered, but the pConst memory location can to where it is pointing to.</span>
<span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span> pConst <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>Value<span style="color: #008080;">;</span>
<span style="color: #666666;">// here the pConst value pointed to can be altered, but the memory location for the pConst for where it is pointing to cannot be altered (always pointing to the same place).</span>
<span style="color: #666666;">// of course you can have both.</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> <span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span> pConstConst <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>constValue<span style="color: #008080;">;</span>
<span style="color: #666666;">// here it is a const int value that has a const pointer that is not allow to change.</span></pre></div></div>

<p>You can also place const around the function definition as well, there are three places where you can place the const type.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> returnValue<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> value1<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span></pre></div></div>

<p>In the order of the const on the function definition list</p>
<ol>
<li>constant return type, cannot alter the returned value (unless you place it in a another variable)</li>
<li>constant parameter passed, you cannot alter the value1 in this case</li>
<li>constant &#8220;this&#8221;, this is the class that it is placed in and you cannot alter any values within that class</li>
</ol>
<p>I have placed below some code that will hopefully explain more for the different types and also the error codes that come if you try and compile up if you are not obeying const rules and I placed the code below for what caused the error.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// there are 3 different places you can put a const (constant)</span>
<span style="color: #666666;">// restriction on a function definition line.</span>
<span style="color: #666666;">// const int returnConstInt(const int value1) const</span>
<span style="color: #666666;">// {</span>
<span style="color: #666666;">// }</span>
<span style="color: #666666;">// in order of placement in the function definition line</span>
<span style="color: #666666;">// 1: cannot alter the returning value</span>
<span style="color: #666666;">// 2: cannot alter the passing value</span>
<span style="color: #666666;">// 3: cannot alter the values of &quot;this&quot; as being the class this</span>
&nbsp;
<span style="color: #0000ff;">class</span> constTest <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">private</span> <span style="color: #008080;">:</span> 
    <span style="color: #0000ff;">int</span> value<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    constTest<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> value <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">int</span> returnIntConst<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> passingValue<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// cannot alter any value for the class variables e.g. value in this case.</span>
<span style="color: #666666;">// of course you could do a const_cast.. which takes off the constant (const) type</span>
<span style="color: #0000ff;">int</span> constTest<span style="color: #008080;">::</span><span style="color: #007788;">returnIntConst</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> passingValue<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// cannot alter any value inside the function</span>
    <span style="color: #666666;">// assignment of data-member ‘constTest::value’ in read-only structure</span>
    <span style="color: #666666;">// value = 3;</span>
    <span style="color: #0000ff;">const_cast</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">&amp;</span><span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> passingValue<span style="color: #008080;">;</span>	<span style="color: #666666;">// take off the const type restriction</span>
    <span style="color: #0000ff;">return</span> value<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> returnInt<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> value1<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #666666;">// cannot alter the parameter value1</span>
  <span style="color: #666666;">//error: assignment of read-only parameter ‘value1’</span>
  <span style="color: #666666;">// value1 = value1+ 2;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span>value1 <span style="color: #000040;">+</span> <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// returning a constant value, which means that you cannot do anything to the </span>
<span style="color: #666666;">// returning value, but if you assign that value to another variable you can</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> returnConstInt<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> value1<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    value1 <span style="color: #000080;">=</span> value1 <span style="color: #000040;">+</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> value1<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// cannot alter the return value of the returnConstInt </span>
    <span style="color: #666666;">//  increment of read-only location ‘returnConstInt(3)’</span>
    <span style="color: #666666;">// int constValue = returnConstInt(3)++;</span>
    <span style="color: #0000ff;">int</span> constValue <span style="color: #000080;">=</span> returnConstInt<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">// but you can alter the value of returning int from returnConstInt if passed to a variable</span>
    constValue<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> constValue <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    constTest cTest<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> cTest.<span style="color: #007788;">returnIntConst</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">10</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>and the output would be</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000;">6</span>
<span style="color: #000000;">10</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/01/29/const-constants-in-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

