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 194 — #232 ✐ Capítulo 6. Inicialización y limpieza }///:~ En el código anterior, tanto el goto como el switch pueden saltar la sentencia en la que se invoca un constructor. Ese objeto corresponde al ámbito incluso si no se invoca el constructor, de modo que el compilador dará un mensaje de error. Esto garantiza de nuevo que un objeto no se puede crear si no se inicializa. Todo el espacio de almacenamiento necesario se asigna en la pila, por supuesto. Ese espacio lo faciliza el compilador moviendo el puntero de pila «hacia abajo» (dependiendo de la máquina implica incrementar o decrementar el valor del puntero de pila). Los objetos también se pueden alojar en el montículo usando new, algo que se verá en el capítulo 13. (FIXME:Ref C13) 6.4. Stash con constructores y destructores Los ejemplos de los capítulos anteriores tienen funciones que tienen correspondencia directa con constructores y destructores: initialize() y cleanup(). Éste es el fichero de cabecera de Stash, utilizando constructor y destructor: //: C06:Stash2.h // With constructors & destructors #ifndef STASH2_H #define STASH2_H class Stash { int size; // Size of each space int quantity; // Number of storage spaces int next; // Next empty space // Dynamically allocated array of bytes: unsigned char* storage; void inflate(int increase); public: Stash(int size); ~Stash(); int add(void* element); void* fetch(int index); int count(); }; #endif // STASH2_H ///:~ Las únicas definiciones de métodos que han cambiado son initialize() y c- leanup(), que han sido reemplazadas con un constructor y un destructor. //: C06:Stash2.cpp {O} // Constructors & destructors #include "Stash2.h" #include "../require.h" #include #include using namespace std; const int increment = 100; Stash::Stash(int sz) { 194 ✐ ✐ ✐ ✐
✐ ✐ ✐ “Volumen1” — 2012/1/12 — 13:52 — page 195 — #233 ✐ 6.4. Stash con constructores y destructores } size = sz; quantity = 0; storage = 0; next = 0; int Stash::add(void* element) { if(next >= quantity) // Enough space left inflate(increment); // Copy element into storage, // starting at next empty space: int startBytes = next * size; unsigned char* e = (unsigned char*)element; for(int i = 0; i < size; i++) storage[startBytes + i] = e[i]; next++; return(next - 1); // Index number } void* Stash::fetch(int index) { require(0 = next) return 0; // To indicate the end // Produce pointer to desired element: return &(storage[index * size]); } int Stash::count() { return next; // Number of elements in CStash } void Stash::inflate(int increase) { require(increase > 0, "Stash::inflate zero or negative increase"); int newQuantity = quantity + increase; int newBytes = newQuantity * size; int oldBytes = quantity * size; unsigned char* b = new unsigned char[newBytes]; for(int i = 0; i < oldBytes; i++) b[i] = storage[i]; // Copy old to new delete [](storage); // Old storage storage = b; // Point to new memory quantity = newQuantity; } Stash::~Stash() { if(storage != 0) { cout
- Page 181 and 182: ✐ ✐ ✐ “Volumen1” — 2012
- Page 183 and 184: ✐ ✐ ✐ “Volumen1” — 2012
- Page 185 and 186: ✐ ✐ ✐ “Volumen1” — 2012
- Page 187 and 188: ✐ ✐ ✐ “Volumen1” — 2012
- Page 189 and 190: ✐ ✐ ✐ “Volumen1” — 2012
- Page 191 and 192: ✐ ✐ ✐ “Volumen1” — 2012
- Page 193 and 194: ✐ ✐ ✐ “Volumen1” — 2012
- Page 195 and 196: ✐ ✐ ✐ “Volumen1” — 2012
- Page 197 and 198: ✐ ✐ ✐ “Volumen1” — 2012
- Page 199 and 200: ✐ ✐ ✐ “Volumen1” — 2012
- Page 201 and 202: ✐ ✐ ✐ “Volumen1” — 2012
- Page 203 and 204: ✐ ✐ ✐ “Volumen1” — 2012
- Page 205 and 206: ✐ ✐ ✐ “Volumen1” — 2012
- Page 207 and 208: ✐ ✐ ✐ “Volumen1” — 2012
- Page 209 and 210: ✐ ✐ ✐ “Volumen1” — 2012
- Page 211 and 212: ✐ ✐ ✐ “Volumen1” — 2012
- Page 213 and 214: ✐ ✐ ✐ “Volumen1” — 2012
- Page 215 and 216: ✐ ✐ ✐ “Volumen1” — 2012
- Page 217 and 218: ✐ ✐ ✐ “Volumen1” — 2012
- Page 219 and 220: ✐ ✐ ✐ “Volumen1” — 2012
- Page 221 and 222: ✐ ✐ ✐ “Volumen1” — 2012
- Page 223 and 224: ✐ ✐ ✐ “Volumen1” — 2012
- Page 225 and 226: ✐ ✐ ✐ “Volumen1” — 2012
- Page 227 and 228: ✐ ✐ ✐ “Volumen1” — 2012
- Page 229 and 230: ✐ ✐ ✐ “Volumen1” — 2012
- Page 231: ✐ ✐ ✐ “Volumen1” — 2012
- Page 235 and 236: ✐ ✐ ✐ “Volumen1” — 2012
- Page 237 and 238: ✐ ✐ ✐ “Volumen1” — 2012
- Page 239 and 240: ✐ ✐ ✐ “Volumen1” — 2012
- Page 241 and 242: ✐ ✐ ✐ “Volumen1” — 2012
- Page 243 and 244: ✐ ✐ ✐ “Volumen1” — 2012
- Page 245 and 246: ✐ ✐ ✐ “Volumen1” — 2012
- Page 247 and 248: ✐ ✐ ✐ “Volumen1” — 2012
- Page 249 and 250: ✐ ✐ ✐ “Volumen1” — 2012
- Page 251 and 252: ✐ ✐ ✐ “Volumen1” — 2012
- Page 253 and 254: ✐ ✐ ✐ “Volumen1” — 2012
- Page 255 and 256: ✐ ✐ ✐ “Volumen1” — 2012
- Page 257 and 258: ✐ ✐ ✐ “Volumen1” — 2012
- Page 259 and 260: ✐ ✐ ✐ “Volumen1” — 2012
- Page 261 and 262: ✐ ✐ ✐ “Volumen1” — 2012
- Page 263 and 264: ✐ ✐ ✐ “Volumen1” — 2012
- Page 265 and 266: ✐ ✐ ✐ “Volumen1” — 2012
- Page 267 and 268: ✐ ✐ ✐ “Volumen1” — 2012
- Page 269 and 270: ✐ ✐ ✐ “Volumen1” — 2012
- Page 271 and 272: ✐ ✐ ✐ “Volumen1” — 2012
- Page 273 and 274: ✐ ✐ ✐ “Volumen1” — 2012
- Page 275 and 276: ✐ ✐ ✐ “Volumen1” — 2012
- Page 277 and 278: ✐ ✐ ✐ “Volumen1” — 2012
- Page 279 and 280: ✐ ✐ ✐ “Volumen1” — 2012
- Page 281 and 282: ✐ ✐ ✐ “Volumen1” — 2012
✐<br />
✐<br />
✐<br />
“Volum<strong>en</strong>1” — 2012/1/12 — 13:52 — page 195 — #233<br />
✐<br />
6.4. Stash con constructores y destructores<br />
}<br />
size = sz;<br />
quantity = 0;<br />
storage = 0;<br />
next = 0;<br />
int Stash::add(void* elem<strong>en</strong>t) {<br />
if(next >= quantity) // Enough space left<br />
inflate(increm<strong>en</strong>t);<br />
// Copy elem<strong>en</strong>t into storage,<br />
// starting at next empty space:<br />
int startBytes = next * size;<br />
unsigned char* e = (unsigned char*)elem<strong>en</strong>t;<br />
for(int i = 0; i < size; i++)<br />
storage[startBytes + i] = e[i];<br />
next++;<br />
return(next - 1); // Index number<br />
}<br />
void* Stash::fetch(int index) {<br />
require(0 = next)<br />
return 0; // To indicate the <strong>en</strong>d<br />
// Produce pointer to desired elem<strong>en</strong>t:<br />
return &(storage[index * size]);<br />
}<br />
int Stash::count() {<br />
return next; // Number of elem<strong>en</strong>ts in CStash<br />
}<br />
void Stash::inflate(int increase) {<br />
require(increase > 0,<br />
"Stash::inflate zero or negative increase");<br />
int newQuantity = quantity + increase;<br />
int newBytes = newQuantity * size;<br />
int oldBytes = quantity * size;<br />
unsigned char* b = new unsigned char[newBytes];<br />
for(int i = 0; i < oldBytes; i++)<br />
b[i] = storage[i]; // Copy old to new<br />
delete [](storage); // Old storage<br />
storage = b; // Point to new memory<br />
quantity = newQuantity;<br />
}<br />
Stash::~Stash() {<br />
if(storage != 0) {<br />
cout