diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/gcc~13/static-definitions.html | |
new repository
Diffstat (limited to 'devdocs/gcc~13/static-definitions.html')
| -rw-r--r-- | devdocs/gcc~13/static-definitions.html | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/devdocs/gcc~13/static-definitions.html b/devdocs/gcc~13/static-definitions.html new file mode 100644 index 00000000..e529ff8b --- /dev/null +++ b/devdocs/gcc~13/static-definitions.html @@ -0,0 +1,13 @@ +<div class="subsection-level-extent" id="Static-Definitions"> <div class="nav-panel"> <p> Next: <a href="name-lookup" accesskey="n" rel="next">Name Lookup, Templates, and Accessing Members of Base Classes</a>, Up: <a href="c_002b_002b-misunderstandings" accesskey="u" rel="up">Common Misunderstandings with GNU C++</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div> <h1 class="subsection" id="Declare-and-Define-Static-Members"><span>14.7.1 Declare <em class="emph">and</em> Define Static Members<a class="copiable-link" href="#Declare-and-Define-Static-Members"> ¶</a></span></h1> <p>When a class has static data members, it is not enough to <em class="emph">declare</em> the static member; you must also <em class="emph">define</em> it. For example: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">class Foo +{ + … + void method(); + static int bar; +};</pre> +</div> <p>This declaration only establishes that the class <code class="code">Foo</code> has an <code class="code">int</code> named <code class="code">Foo::bar</code>, and a member function named <code class="code">Foo::method</code>. But you still need to define <em class="emph">both</em> <code class="code">method</code> and <code class="code">bar</code> elsewhere. According to the ISO standard, you must supply an initializer in one (and only one) source file, such as: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">int Foo::bar = 0;</pre> +</div> <p>Other C++ compilers may not correctly implement the standard behavior. As a result, when you switch to <code class="command">g++</code> from one of these compilers, you may discover that a program that appeared to work correctly in fact does not conform to the standard: <code class="command">g++</code> reports as undefined symbols any static data members that lack definitions. </p> </div><div class="_attribution"> + <p class="_attribution-p"> + © Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br> + <a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Static-Definitions.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Static-Definitions.html</a> + </p> +</div> |
