Pensar en C++ (Volumen 1) - Grupo ARCO
Pensar en C++ (Volumen 1) - Grupo ARCO Pensar en C++ (Volumen 1) - Grupo ARCO
✐ ✐ ✐ “Volumen1” — 2012/1/12 — 13:52 — page 296 — #334 ✐ Capítulo 10. Control de nombres plataforma determinada y, de golpe y misteriosamente, compilarlo en otro entorno y que deje de funcionar. 10.4.1. Qué hacer Existen tres aproximaciones para tratar con este problema: 1. No hacerlo. Evitar las dependencias de inicialización estática es la mejor solución. 2. Si debe hacerlo, coloque las definiciones de objetos estáticos críticos en un único fichero, de forma que pueda controlar, de forma portable, su inicialización colocándolos en el orden correcto. 3. Si está convencido que es inevitable dispersar objetos estáticos entre unidades de traducción diferentes (como en el caso de una librería, donde no puede controlar el programa que la usa), hay dos técnicas de programación para solventar el problema. Técnica uno El pionero de esta técnica fue Jerry Schwarz mientras creaba la librería iostream (puesto que las definiciones para cin, cout y cerr son static y residen en archivos diferentes). Realmente es inferior a la segunda técnica pero ha pululado durante mucho tiempo por lo que puede encontrarse con código que la utilice; así pues, es importante que entienda como funciona. Esta técnica requiere una clase adicional en su archivo de cabecera. Esta clase es la responsable de la inicialización dinámica de sus objetos estáticos de librería. He aquí un ejemplo simple: //: C10:Initializer.h // Static initialization technique #ifndef INITIALIZER_H #define INITIALIZER_H #include extern int x; // Declarations, not definitions extern int y; class Initializer { static int initCount; public: Initializer() { std::cout
✐ ✐ ✐ “Volumen1” — 2012/1/12 — 13:52 — page 297 — #335 ✐ 10.4. Dependencia en la inicialización de variables estáticas } }; } std::cout
- Page 283 and 284: ✐ ✐ ✐ “Volumen1” — 2012
- Page 285 and 286: ✐ ✐ ✐ “Volumen1” — 2012
- Page 287 and 288: ✐ ✐ ✐ “Volumen1” — 2012
- Page 289 and 290: ✐ ✐ ✐ “Volumen1” — 2012
- Page 291 and 292: ✐ ✐ ✐ “Volumen1” — 2012
- Page 293 and 294: ✐ ✐ ✐ “Volumen1” — 2012
- Page 295 and 296: ✐ ✐ ✐ “Volumen1” — 2012
- Page 297 and 298: ✐ ✐ ✐ “Volumen1” — 2012
- Page 299 and 300: ✐ ✐ ✐ “Volumen1” — 2012
- Page 301 and 302: ✐ ✐ ✐ “Volumen1” — 2012
- Page 303 and 304: ✐ ✐ ✐ “Volumen1” — 2012
- Page 305 and 306: ✐ ✐ ✐ “Volumen1” — 2012
- Page 307 and 308: ✐ ✐ ✐ “Volumen1” — 2012
- Page 309 and 310: ✐ ✐ ✐ “Volumen1” — 2012
- Page 311 and 312: ✐ ✐ ✐ “Volumen1” — 2012
- Page 313 and 314: ✐ ✐ ✐ “Volumen1” — 2012
- Page 315 and 316: ✐ ✐ ✐ “Volumen1” — 2012
- Page 317 and 318: ✐ ✐ ✐ “Volumen1” — 2012
- Page 319 and 320: ✐ ✐ ✐ “Volumen1” — 2012
- Page 321 and 322: ✐ ✐ ✐ “Volumen1” — 2012
- Page 323 and 324: ✐ ✐ ✐ “Volumen1” — 2012
- Page 325 and 326: ✐ ✐ ✐ “Volumen1” — 2012
- Page 327 and 328: ✐ ✐ ✐ “Volumen1” — 2012
- Page 329 and 330: ✐ ✐ ✐ “Volumen1” — 2012
- Page 331 and 332: ✐ ✐ ✐ “Volumen1” — 2012
- Page 333: ✐ ✐ ✐ “Volumen1” — 2012
- Page 337 and 338: ✐ ✐ ✐ “Volumen1” — 2012
- Page 339 and 340: ✐ ✐ ✐ “Volumen1” — 2012
- Page 341 and 342: ✐ ✐ ✐ “Volumen1” — 2012
- Page 343 and 344: ✐ ✐ ✐ “Volumen1” — 2012
- Page 345 and 346: ✐ ✐ ✐ “Volumen1” — 2012
- Page 347 and 348: ✐ ✐ ✐ “Volumen1” — 2012
- Page 349 and 350: ✐ ✐ ✐ “Volumen1” — 2012
- Page 351 and 352: ✐ ✐ ✐ “Volumen1” — 2012
- Page 353 and 354: ✐ ✐ ✐ “Volumen1” — 2012
- Page 355 and 356: ✐ ✐ ✐ “Volumen1” — 2012
- Page 357 and 358: ✐ ✐ ✐ “Volumen1” — 2012
- Page 359 and 360: ✐ ✐ ✐ “Volumen1” — 2012
- Page 361 and 362: ✐ ✐ ✐ “Volumen1” — 2012
- Page 363 and 364: ✐ ✐ ✐ “Volumen1” — 2012
- Page 365 and 366: ✐ ✐ ✐ “Volumen1” — 2012
- Page 367 and 368: ✐ ✐ ✐ “Volumen1” — 2012
- Page 369 and 370: ✐ ✐ ✐ “Volumen1” — 2012
- Page 371 and 372: ✐ ✐ ✐ “Volumen1” — 2012
- Page 373 and 374: ✐ ✐ ✐ “Volumen1” — 2012
- Page 375 and 376: ✐ ✐ ✐ “Volumen1” — 2012
- Page 377 and 378: ✐ ✐ ✐ “Volumen1” — 2012
- Page 379 and 380: ✐ ✐ ✐ “Volumen1” — 2012
- Page 381 and 382: ✐ ✐ ✐ “Volumen1” — 2012
- Page 383 and 384: ✐ ✐ ✐ “Volumen1” — 2012
✐<br />
✐<br />
✐<br />
“Volum<strong>en</strong>1” — 2012/1/12 — 13:52 — page 297 — #335<br />
✐<br />
10.4. Dep<strong>en</strong>d<strong>en</strong>cia <strong>en</strong> la inicialización de variables estáticas<br />
}<br />
};<br />
}<br />
std::cout