Pensar en C++ (Volumen 1) - Grupo ARCO
Pensar en C++ (Volumen 1) - Grupo ARCO
Pensar en C++ (Volumen 1) - Grupo ARCO
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
✐<br />
✐<br />
✐<br />
“Volum<strong>en</strong>1” — 2012/1/12 — 13:52 — page 92 — #130<br />
✐<br />
Capítulo 3. C <strong>en</strong> <strong>C++</strong><br />
// linking this file with FileStatic2.cpp<br />
// will cause a linker error<br />
// File scope means only available in this file:<br />
static int fs;<br />
int main() {<br />
fs = 1;<br />
} ///:~<br />
Aunque la variable fs está destinada a existir como un extern <strong>en</strong> el sigui<strong>en</strong>te fichero,<br />
el <strong>en</strong>lazador no la <strong>en</strong>contraría porque ha sido declarada static <strong>en</strong> FileStatic.<br />
cpp.<br />
//: C03:FileStatic2.cpp {O}<br />
// Trying to refer<strong>en</strong>ce fs<br />
extern int fs;<br />
void func() {<br />
fs = 100;<br />
} ///:~<br />
El especificador static también se puede usar d<strong>en</strong>tro de una clase. Esta explicación<br />
se dará más adelante <strong>en</strong> este libro, cuando apr<strong>en</strong>da a crear clases.<br />
3.6.4. extern<br />
La palabra reservada extern ya ha sido brevem<strong>en</strong>te descripta. Le dice al compilador<br />
que una variable o una función existe, incluso si el compilado aún no la ha<br />
visto <strong>en</strong> el fichero que está si<strong>en</strong>do compilado <strong>en</strong> ese mom<strong>en</strong>to. Esta variable o función<br />
puede definirse <strong>en</strong> otro fichero o más abajo <strong>en</strong> el fichero actual. A modo de<br />
ejemplo:<br />
//: C03:Forward.cpp<br />
// Forward function & data declarations<br />
#include <br />
using namespace std;<br />
// This is not actually external, but the<br />
// compiler must be told it exists somewhere:<br />
extern int i;<br />
extern void func();<br />
int main() {<br />
i = 0;<br />
func();<br />
}<br />
int i; // The data definition<br />
void func() {<br />
i++;<br />
cout