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 366 — #404 ✐ Capítulo 12. Sobrecarga de operadores Cargo& operator=(const Cargo&) { cout
✐ ✐ ✐ “Volumen1” — 2012/1/12 — 13:52 — page 367 — #405 ✐ 12.6. Conversión automática de tipos int main() { One one; f(one); // Wants a Two, has a One } ///:~ Cuando el compilador ve que f() es invocada pasando un objeto One, mira en la declaración de f() y ve que requiere un Two. Entonces busca si hay alguna manera de conseguir un Two a partir de un One, encuentra el constructor Two::Two(One) y lo llama. Pasa el objeto Two resultante a f(). En este caso, la conversión automática de tipos le ha salvado del problema de definir dos versiones sobrecargadas de f(). Sin embargo el coste es la llamada oculta al constructor de Two, que puede ser importante si está preocupado por la eficiencia de las llamadas a f(), Evitar la conversión por constructor Hay veces en que la conversión automática de tipos vía constructor puede ocasionar problemas. Para desactivarlo, modifique el constructor anteponiéndole la palabra reservada explicit (que sólo funciona con constructores). Así se ha hecho para modificar el constructor de la clase Two en el ejemplo anterior: //: C12:ExplicitKeyword.cpp // Using the "explicit" keyword class One { public: One() {} }; class Two { public: explicit Two(const One&) {} }; void f(Two) {} int main() { One one; //! f(one); // No auto conversion allowed f(Two(one)); // OK -- user performs conversion } ///:~ Haciendo el constructor de Two explícito, se le dice al compilador que no realice ninguna conversión automática de tipos usando ese constructor en particular (sí se podrían usar otros constructores no explícitos de esa clase para realizar conversiones automáticas). Si el usuario quiere que ocurra esa conversión, debe escribir el codigo necesario. En el código de arriba, f(Two(one)) crea un objeto temporal de tipo T- wo a partir de one, justo como el compilador hizo automáticamente en la versión anterior. 367 ✐ ✐ ✐ ✐
- 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
- Page 385 and 386: ✐ ✐ ✐ “Volumen1” — 2012
- Page 387 and 388: ✐ ✐ ✐ “Volumen1” — 2012
- Page 389 and 390: ✐ ✐ ✐ “Volumen1” — 2012
- Page 391 and 392: ✐ ✐ ✐ “Volumen1” — 2012
- Page 393 and 394: ✐ ✐ ✐ “Volumen1” — 2012
- Page 395 and 396: ✐ ✐ ✐ “Volumen1” — 2012
- Page 397 and 398: ✐ ✐ ✐ “Volumen1” — 2012
- Page 399 and 400: ✐ ✐ ✐ “Volumen1” — 2012
- Page 401 and 402: ✐ ✐ ✐ “Volumen1” — 2012
- Page 403: ✐ ✐ ✐ “Volumen1” — 2012
- Page 407 and 408: ✐ ✐ ✐ “Volumen1” — 2012
- Page 409 and 410: ✐ ✐ ✐ “Volumen1” — 2012
- Page 411 and 412: ✐ ✐ ✐ “Volumen1” — 2012
- Page 413 and 414: ✐ ✐ ✐ “Volumen1” — 2012
- Page 415 and 416: ✐ ✐ ✐ “Volumen1” — 2012
- Page 417 and 418: ✐ ✐ ✐ “Volumen1” — 2012
- Page 419 and 420: ✐ ✐ ✐ “Volumen1” — 2012
- Page 421 and 422: ✐ ✐ ✐ “Volumen1” — 2012
- Page 423 and 424: ✐ ✐ ✐ “Volumen1” — 2012
- Page 425 and 426: ✐ ✐ ✐ “Volumen1” — 2012
- Page 427 and 428: ✐ ✐ ✐ “Volumen1” — 2012
- Page 429 and 430: ✐ ✐ ✐ “Volumen1” — 2012
- Page 431 and 432: ✐ ✐ ✐ “Volumen1” — 2012
- Page 433 and 434: ✐ ✐ ✐ “Volumen1” — 2012
- Page 435 and 436: ✐ ✐ ✐ “Volumen1” — 2012
- Page 437 and 438: ✐ ✐ ✐ “Volumen1” — 2012
- Page 439 and 440: ✐ ✐ ✐ “Volumen1” — 2012
- Page 441 and 442: ✐ ✐ ✐ “Volumen1” — 2012
- Page 443 and 444: ✐ ✐ ✐ “Volumen1” — 2012
- Page 445 and 446: ✐ ✐ ✐ “Volumen1” — 2012
- Page 447 and 448: ✐ ✐ ✐ “Volumen1” — 2012
- Page 449 and 450: ✐ ✐ ✐ “Volumen1” — 2012
- Page 451 and 452: ✐ ✐ ✐ “Volumen1” — 2012
- Page 453 and 454: ✐ ✐ ✐ “Volumen1” — 2012
✐<br />
✐<br />
✐<br />
“Volum<strong>en</strong>1” — 2012/1/12 — 13:52 — page 366 — #404<br />
✐<br />
Capítulo 12. Sobrecarga de operadores<br />
Cargo& operator=(const Cargo&) {<br />
cout