13.01.2015 Views

Pensar en C++ (Volumen 1) - Grupo ARCO

Pensar en C++ (Volumen 1) - Grupo ARCO

Pensar en C++ (Volumen 1) - Grupo ARCO

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

✐<br />

✐<br />

✐<br />

“Volum<strong>en</strong>1” — 2012/1/12 — 13:52 — page 208 — #246<br />

✐<br />

Capítulo 7. Sobrecarga de funciones y argum<strong>en</strong>tos por defecto<br />

En el segundo fichero, la función está mal declarada y <strong>en</strong> main se le llama:<br />

//: C07:Use.cpp<br />

//{L} Def<br />

// Function misdeclaration<br />

void f(char);<br />

int main() {<br />

//! f(1); // Causes a linker error<br />

} ///:~<br />

Incluso aunque pueda ver que la función es realm<strong>en</strong>te f(int), el compilador<br />

no lo sabe porque se le dijo, a través de una declaración explícita, que la función es<br />

f(char). Así pues, la compilación ti<strong>en</strong>e éxito. En C, el <strong>en</strong>lazador podría t<strong>en</strong>er también<br />

éxito, pero no <strong>en</strong> <strong>C++</strong>. Como el compilador decora los nombres, la definición se<br />

convierte <strong>en</strong> algo así como f_int, mi<strong>en</strong>tras que se trata de utilizar f_char. Cuando<br />

el <strong>en</strong>lazador int<strong>en</strong>ta resolver la refer<strong>en</strong>cia a f_char, sólo puede <strong>en</strong>contrar f_int,<br />

y da un m<strong>en</strong>saje de error. Éste es el <strong>en</strong>lace de tipos seguro. Aunque el problema no<br />

ocurre muy a m<strong>en</strong>udo, cuando ocurre puede ser increíblem<strong>en</strong>te difícil de <strong>en</strong>contrar,<br />

especialm<strong>en</strong>te <strong>en</strong> proyectos grandes. Éste método puede utilizarse para <strong>en</strong>contrar<br />

un error <strong>en</strong> C simplem<strong>en</strong>te int<strong>en</strong>tando compilarlo <strong>en</strong> <strong>C++</strong>.<br />

7.2. Ejemplo de sobrecarga<br />

Ahora puede modificar ejemplos anteriores para utilizar la sobrecarga de funciones.<br />

Como ya se dijo, el lugar inmediatam<strong>en</strong>te más útil para la sobrecarga es <strong>en</strong> los<br />

constructores. Puede comprobarlo <strong>en</strong> la sigui<strong>en</strong>te versión de la clase Stash:<br />

//: C07:Stash3.h<br />

// Function overloading<br />

#ifndef STASH3_H<br />

#define STASH3_H<br />

class Stash {<br />

int size; // Size of each space<br />

int quantity; // Number of storage spaces<br />

int next; // Next empty space<br />

// Dynamically allocated array of bytes:<br />

unsigned char* storage;<br />

void inflate(int increase);<br />

public:<br />

Stash(int size); // Zero quantity<br />

Stash(int size, int initQuantity);<br />

~Stash();<br />

int add(void* elem<strong>en</strong>t);<br />

void* fetch(int index);<br />

int count();<br />

};<br />

#<strong>en</strong>dif // STASH3_H ///:~<br />

208<br />

✐<br />

✐<br />

✐<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!