<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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>
static [C++ Reference]
</title>
<meta name="generator" content="DokuWiki Release 2009-12-25c "Lemming"" />
<meta name="robots" content="index,follow" />
<meta name="date" content="2010-03-17T11:31:29-0700" />
<meta name="keywords" content="keywords,static" />
<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/static?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&ns=keywords" />
<link rel="edit" title="Edit this page" href="/wiki/keywords/static?do=edit" />
<link rel="alternate" type="text/html" title="Plain HTML" href="/wiki/_export/xhtml/keywords/static" />
<link rel="alternate" type="text/plain" title="Wiki Markup" href="/wiki/_export/raw/keywords/static" />
<link rel="canonical" href="http://www.cppreference.com/wiki/keywords/static" />
<link rel="stylesheet" media="all" type="text/css" href="/wiki/lib/exe/css.php?s=all&t=custom1&tseed=1272971091" />
<link rel="stylesheet" media="screen" type="text/css" href="/wiki/lib/exe/css.php?t=custom1&tseed=1272971091" />
<link rel="stylesheet" media="print" type="text/css" href="/wiki/lib/exe/css.php?s=print&t=custom1&tseed=1272971091" />
<script type="text/javascript" charset="utf-8" ><!--//--><![CDATA[//><!--
var NS='keywords';var JSINFO = {"id":"keywords:static","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> » <a href="../keywords/start.html" title="keywords:start">C++ Keywords</a> » <a href="../keywords/static.html" title="keywords:static">static</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="#static" class="toc">static</a></span></div>
<ul class="toc">
<li class="level3"><div class="li"><span class="li"><a href="#permanent_storage" class="toc">Permanent storage</a></span></div></li>
<li class="level3"><div class="li"><span class="li"><a href="#single_copy_of_class_data" class="toc">Single copy of class data</a></span></div></li>
<li class="level3"><div class="li"><span class="li"><a href="#class_functions_callable_without_an_object" class="toc">Class functions callable without an object</a></span></div></li>
<li class="level3"><div class="li"><span class="li"><a href="#internal_linkage" class="toc">Internal linkage</a></span></div></li></ul>
</li></ul>
</li></ul>
</div>
</div>
<!-- TOC END -->
<h2><a name="static" id="static">static</a></h2>
<div class="level2">
<p>
The static keyword can be used in four different ways:
</p>
<ol>
<li class="level1"><div class="li"> to create permanent storage for local variables in a function,</div>
</li>
<li class="level1"><div class="li"> to create a single copy of class data,</div>
</li>
<li class="level1"><div class="li"> to declare member functions that act like non-member functions, and</div>
</li>
<li class="level1"><div class="li"> to specify internal linkage.</div>
</li>
</ol>
</div>
<h3><a name="permanent_storage" id="permanent_storage">Permanent storage</a></h3>
<div class="level3">
<p>
Static local variables keep their value between function calls. For example, in the following code, a static variable inside a function is used to keep track of how many times that function has been called:
</p>
<pre class="c code c++" style="font-family:monospace;"><span class="kw4">void</span> foo<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
<span class="kw4">static</span> <span class="kw4">int</span> counter <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span>
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> <span class="sy0"><<</span> <span class="st0">"foo has been called "</span> <span class="sy0"><<</span> <span class="sy0">++</span>counter <span class="sy0"><<</span> <span class="st0">" times<span class="es1">\n</span>"</span><span class="sy0">;</span>
<span class="br0">}</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
<span class="kw1">for</span><span class="br0">(</span> <span class="kw4">int</span> i <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> i <span class="sy0"><</span> <span class="nu0">10</span><span class="sy0">;</span> <span class="sy0">++</span>i <span class="br0">)</span> foo<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span>
<span class="br0">}</span></pre>
</div>
<h3><a name="single_copy_of_class_data" id="single_copy_of_class_data">Single copy of class data</a></h3>
<div class="level3">
<p>
When used in a class data member, all instantiations of that class share one copy of the variable.
</p>
<pre class="c code c++" style="font-family:monospace;">class Foo <span class="br0">{</span>
public<span class="sy0">:</span>
Foo<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
<span class="sy0">++</span>numFoos<span class="sy0">;</span>
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> <span class="sy0"><<</span> <span class="st0">"We have now created "</span> <span class="sy0"><<</span> numFoos <span class="sy0"><<</span> <span class="st0">" instances of the Foo class<span class="es1">\n</span>"</span><span class="sy0">;</span>
<span class="br0">}</span>
private<span class="sy0">:</span>
<span class="kw4">static</span> <span class="kw4">int</span> numFoos<span class="sy0">;</span>
<span class="br0">}</span><span class="sy0">;</span>
<span class="kw4">int</span> Foo<span class="sy0">::</span><span class="me2">numFoos</span> <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> <span class="co1">// allocate memory for numFoos, and initialize it</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
Foo f1<span class="sy0">;</span>
Foo f2<span class="sy0">;</span>
Foo f3<span class="sy0">;</span>
<span class="br0">}</span></pre>
<p>
In the example above, the static class variable numFoos is shared between all three instances of the Foo class (f1, f2 and f3) and keeps a count of the number of times that the Foo class has been instantiated.
</p>
</div>
<h3><a name="class_functions_callable_without_an_object" id="class_functions_callable_without_an_object">Class functions callable without an object</a></h3>
<div class="level3">
<p>
When used in a class function member, the function does not take an instantiation as an implicit <a href="../keywords/this.html" class="wikilink1" title="keywords:this">this</a> parameter, instead behaving like a free function. This means that static class functions can be called without creating instances of the class:
</p>
<pre class="c code c++" style="font-family:monospace;">class Foo <span class="br0">{</span>
public<span class="sy0">:</span>
Foo<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
<span class="sy0">++</span>numFoos<span class="sy0">;</span>
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> <span class="sy0"><<</span> <span class="st0">"We have now created "</span> <span class="sy0"><<</span> numFoos <span class="sy0"><<</span> <span class="st0">" instances of the Foo class<span class="es1">\n</span>"</span><span class="sy0">;</span>
<span class="br0">}</span>
<span class="kw4">static</span> <span class="kw4">int</span> getNumFoos<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
<span class="kw1">return</span> numFoos<span class="sy0">;</span>
<span class="br0">}</span>
private<span class="sy0">:</span>
<span class="kw4">static</span> <span class="kw4">int</span> numFoos<span class="sy0">;</span>
<span class="br0">}</span><span class="sy0">;</span>
<span class="kw4">int</span> Foo<span class="sy0">::</span><span class="me2">numFoos</span> <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> <span class="co1">// allocate memory for numFoos, and initialize it</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
Foo f1<span class="sy0">;</span>
Foo f2<span class="sy0">;</span>
Foo f3<span class="sy0">;</span>
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> <span class="sy0"><<</span> <span class="st0">"So far, we've made "</span> <span class="sy0"><<</span> Foo<span class="sy0">::</span><span class="me2">getNumFoos</span><span class="br0">(</span><span class="br0">)</span> <span class="sy0"><<</span> <span class="st0">" instances of the Foo class<span class="es1">\n</span>"</span><span class="sy0">;</span>
<span class="br0">}</span></pre>
</div>
<h3><a name="internal_linkage" id="internal_linkage">Internal linkage</a></h3>
<div class="level3">
<p>
When used on a free function, a global variable, or a global constant, it specifies internal linkage (as opposed to <a href="../keywords/extern.html" class="wikilink1" title="keywords:extern">extern</a>, which specifies external linkage). Internal linkage limits access to the data or function to the current file.
</p>
<p>
Related: <a href="../keywords/extern.html" class="wikilink1" title="keywords:extern">extern</a>
</p>
</div>
<!-- wikipage stop -->
</div>
<div class="clearer"> </div>
<div class="stylefoot">
<div class="meta">
<div class="user">
</div>
<!--
<div class="doc">
keywords/static.txt · Last modified: 03/17/2010 11:31 by nate </div>
-->
</div>
</div></div></body>
</html>