<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
const [C++ Reference]
</title>
<meta name="generator" content="DokuWiki Release 2009-12-25c &quot;Lemming&quot;" />
<meta name="robots" content="index,follow" />
<meta name="date" content="2009-04-20T18:23:47-0700" />
<meta name="keywords" content="keywords,const" />
<link rel="search" type="application/opensearchdescription+xml" href="/wiki/lib/exe/opensearch.php" title="C++ Reference" />
<link rel="start" href="/wiki/" />
<link rel="contents" href="/wiki/keywords/const?do=index" title="Index" />
<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="/wiki/feed.php" />
<link rel="alternate" type="application/rss+xml" title="Current Namespace" href="/wiki/feed.php?mode=list&amp;ns=keywords" />
<link rel="edit" title="Edit this page" href="/wiki/keywords/const?do=edit" />
<link rel="alternate" type="text/html" title="Plain HTML" href="/wiki/_export/xhtml/keywords/const" />
<link rel="alternate" type="text/plain" title="Wiki Markup" href="/wiki/_export/raw/keywords/const" />
<link rel="canonical" href="http://www.cppreference.com/wiki/keywords/const" />
<link rel="stylesheet" media="all" type="text/css" href="/wiki/lib/exe/css.php?s=all&amp;t=custom1&amp;tseed=1272971091" />
<link rel="stylesheet" media="screen" type="text/css" href="/wiki/lib/exe/css.php?t=custom1&amp;tseed=1272971091" />
<link rel="stylesheet" media="print" type="text/css" href="/wiki/lib/exe/css.php?s=print&amp;t=custom1&amp;tseed=1272971091" />
<script type="text/javascript" charset="utf-8" ><!--//--><![CDATA[//><!--
var NS='keywords';var JSINFO = {"id":"keywords:const","namespace":"keywords"};
//--><!]]></script>
<script type="text/javascript" charset="utf-8" src="/wiki/lib/exe/js.php?tseed=1272971091" ></script>
<link rel="shortcut icon" href="/wiki/lib/tpl/custom1/images/favicon.png" />
</head>
<body>
<div class="dokuwiki">
<div class="stylehead">
<div class="breadcrumbs">
<span class="bchead">You are here: </span><a href="../start.html" title="start">C++ Reference</a> &raquo; <a href="../keywords/start.html" title="keywords:start">C++ Keywords</a> &raquo; <a href="../keywords/const.html" title="keywords:const">const</a> </div>
</div>
<div class="page">
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
<!-- wikipage start -->
<!-- TOC START -->
<div class="toc">
<div class="tocheader toctoggle" id="toc__header">Table of Contents</div>
<div id="toc__inside">
<ul class="toc">
<li class="clear">
<ul class="toc">
<li class="level2"><div class="li"><span class="li"><a href="#const" class="toc">const</a></span></div></li>
<li class="level2"><div class="li"><span class="li"><a href="#usage" class="toc">Usage</a></span></div>
<ul class="toc">
<li class="level3"><div class="li"><span class="li"><a href="#declarations" class="toc">Declarations</a></span></div></li>
<li class="level3"><div class="li"><span class="li"><a href="#parameters" class="toc">Parameters</a></span></div></li>
<li class="level3"><div class="li"><span class="li"><a href="#methods" class="toc">Methods</a></span></div></li></ul>
</li></ul>
</li></ul>
</div>
</div>
<!-- TOC END -->
<h2><a name="const" id="const">const</a></h2>
<div class="level2">
<p>
The const keyword can be used to tell the compiler that a certain variable
should not be modified once it has been initialized.
It can also be used to declare functions of a class that do not alter any class
data.
</p>
</div>
<h2><a name="usage" id="usage">Usage</a></h2>
<div class="level2">
<p>
It is considered good practice to use const wherever appropriate to protect data from being unintentionally overwritten. Attempting to shoehorn const into a program after it has been written will create a cascade effect. It is best to implement const early in the code development cycle. This brings us to the proper declaration and usage of const.
</p>
<p>
The const keyword can take on multiple meanings and be used in a variety of locations (even nonsensical places).
</p>
</div>
<h3><a name="declarations" id="declarations">Declarations</a></h3>
<div class="level3">
<p>
To understand what the const is protecting, read from right to left.
</p>
<pre class="c code c++" style="font-family:monospace;"><span class="kw4">const</span> <span class="kw4">char</span> <span class="sy0">*</span> str<span class="sy0">;</span> <span class="co1">// pointer to characters that cannot be changed (although the pointer can be redirected)</span>
<span class="kw4">char</span> <span class="kw4">const</span> <span class="sy0">*</span> str<span class="sy0">;</span> <span class="co1">// same as above (just an alternate way of writing it)</span>
<span class="kw4">char</span> <span class="sy0">*</span> <span class="kw4">const</span> str<span class="sy0">;</span> <span class="co1">// cannot change the pointer to characters (although the characters themselves can be changed)</span></pre>
<p>
Similarly for C++ references (you cannot apply const to a reference, since a reference cannot be redirected):
</p>
<pre class="c code c++" style="font-family:monospace;"><span class="kw4">const</span> <span class="kw4">char</span> <span class="sy0">&amp;</span> str<span class="sy0">;</span> <span class="co1">// reference to character that cannot be changed</span>
<span class="kw4">char</span> <span class="kw4">const</span> <span class="sy0">&amp;</span> str<span class="sy0">;</span> <span class="co1">// same as above</span></pre>
<p>
This seems easy enough, however more complicated resolutions can be more difficult to interpret. Consider:
</p>
<pre class="c code c++" style="font-family:monospace;"><span class="kw4">char</span> <span class="sy0">*</span> <span class="kw4">const</span> <span class="sy0">*</span> data<span class="sy0">;</span> <span class="co1">// pointer to unchangeable pointer of characters</span>
<span class="kw4">char</span> <span class="kw4">const</span> <span class="sy0">**</span> data<span class="sy0">;</span> <span class="co1">// pointer to pointer of unchangeable characters</span>
<span class="kw4">const</span> <span class="kw4">char</span> <span class="sy0">**</span> data<span class="sy0">;</span> <span class="co1">// pointer to pointer of unchangeable characters</span>
<span class="kw4">char</span> <span class="sy0">**</span> <span class="kw4">const</span> data<span class="sy0">;</span> <span class="co1">// unchangeable pointer to pointer of characters</span>
<span class="kw4">char</span> <span class="sy0">*</span> <span class="kw4">const</span> <span class="sy0">*</span> <span class="kw4">const</span> data<span class="sy0">;</span> <span class="co1">// unchangeable pointer to unchangeable pointer of characters</span></pre>
<p>
There are some who would have you believe that you <strong>MUST</strong> place the const after the type, however you are free to place it either before or after the type, if it is a regular non-pointer type (e.g. “char”). Use the format that matches your existing code or your organizations coding standards. Like anything else, just be consistent. If you want the const to apply to the pointer, you must place const after the asterisk.
</p>
<p>
It is also good practice to declare certain fields of an object to be const if it is a property of the object that does not change over the life of the object.
</p>
</div>
<h3><a name="parameters" id="parameters">Parameters</a></h3>
<div class="level3">
<p>
The most common usage of const is to protect data that is pointed to or referenced:
</p>
<pre class="c code c++" style="font-family:monospace;"><span class="kw4">void</span> func <span class="br0">&#40;</span> <span class="kw4">const</span> MyObject <span class="sy0">*</span> data <span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// MyObject cannot be changed in func</span>
<span class="kw4">void</span> func <span class="br0">&#40;</span> <span class="kw4">const</span> MyObject <span class="sy0">&amp;</span> data <span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// MyObject cannot be changed in func</span></pre>
<p>
Note that the placement of the const before or after the type is irrelevant. The following is equivalent:
</p>
<pre class="c code c++" style="font-family:monospace;"><span class="kw4">void</span> func <span class="br0">&#40;</span> MyObject <span class="kw4">const</span> <span class="sy0">*</span> data <span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// same as ( const MyObject * )</span>
<span class="kw4">void</span> func <span class="br0">&#40;</span> MyObject <span class="kw4">const</span> <span class="sy0">&amp;</span> data <span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// same as ( const MyOjbect &amp; )</span></pre>
<p>
However, placement of the const after the pointer or reference changes what is “const”. A const following a pointer protects only the pointer, not the data to which it points.
</p>
<pre class="c code c++" style="font-family:monospace;"><span class="kw4">void</span> func <span class="br0">&#40;</span> MyObject <span class="sy0">*</span> <span class="kw4">const</span> data <span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// unnecessary protection of the copied pointer to MyObject</span></pre>
<p>
Inside func, you are free to manipulate MyObject, but not the pointer to MyObject. However, since the pointer value is a local to the function (the pointer was passed by value when the function was called), this isn&#039;t helpful, as nobody outside the function will be affected by whether you change the pointer or not anyway.
</p>
<p>
Placing the const after a reference is entirely useless and should be avoided. References cannot be redirected (i.e. they are already implicitly const).
</p>
<pre class="c code c++" style="font-family:monospace;"><span class="kw4">void</span> func <span class="br0">&#40;</span> MyObject <span class="sy0">&amp;</span> <span class="kw4">const</span> data <span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// useless protection of the reference to MyObject</span></pre>
<p>
Here, the const is protecting the reference which can never be manipulated anyway.
</p>
<p>
Sometimes it is useful to return private data from an object. However, we don&#039;t want the private data manipulated outside the class.
</p>
<pre class="c code c++" style="font-family:monospace;"><span class="kw4">const</span> MyObject <span class="sy0">&amp;</span> MyClass<span class="sy0">::</span><span class="me2">func</span> <span class="br0">&#40;</span> MyObject <span class="sy0">&amp;</span> data <span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// the MyObject returned by func cannot be changed</span></pre>
</div>
<h3><a name="methods" id="methods">Methods</a></h3>
<div class="level3">
<p>
Often instance methods do not manipulate any data in the objects. These methods, which are also known as <em>accessors</em>, should be declared const. The effect of this is that the <a href="../keywords/this.html" class="wikilink1" title="keywords:this">this</a> pointer inside the method, instead of being a “MyClass *”, will now be a “const MyClass *”, so that they cannot modify the object through the this pointer.
</p>
<pre class="c code c++" style="font-family:monospace;"><span class="kw4">void</span> MyClass<span class="sy0">::</span><span class="me2">func</span> <span class="br0">&#40;</span> MyOjbect <span class="sy0">&amp;</span> data <span class="br0">&#41;</span> <span class="kw4">const</span><span class="sy0">;</span> <span class="co1">// this function does not manipulate class data</span></pre>
<p>
Note that if you have a const object, you may only call const methods on that object. (The reason is that when you call a method, you need to pass a pointer to the object as the this pointer. But if you have a “const MyClass” object, then you can only get a “const MyClass *”, not a “MyClass *”, so you can only call const methods.)
</p>
<pre class="c code c++" style="font-family:monospace;"><span class="kw4">void</span> MyClass<span class="sy0">::</span><span class="me2">const_func</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw4">const</span><span class="sy0">;</span>
<span class="kw4">void</span> MyClass<span class="sy0">::</span><span class="me2">func</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="kw4">const</span> MyClass object<span class="sy0">;</span>
&nbsp;
object.<span class="me1">const_func</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// ok</span>
object.<span class="me1">func</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// can't call non-const function in a const object</span></pre>
<p>
For the same reason, inside a const method, you can only call other const methods of the object.
</p>
<p>
Related Topics: <a href="../keywords/const_cast.html" class="wikilink1" title="keywords:const_cast">const_cast</a>, <a href="../keywords/mutable.html" class="wikilink1" title="keywords:mutable">mutable</a>
</p>
</div>
<!-- wikipage stop -->
</div>
<div class="clearer">&nbsp;</div>
<div class="stylefoot">
<div class="meta">
<div class="user">
</div>
<!--
<div class="doc">
keywords/const.txt &middot; Last modified: 04/20/2009 18:23 by 121.44.106.32 </div>
-->
</div>
</div></div></body>
</html>