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 308 — #346<br />

✐<br />

Capítulo 11. Las refer<strong>en</strong>cias y el constructor de copia<br />

11.2. Refer<strong>en</strong>cias <strong>en</strong> <strong>C++</strong><br />

Una refer<strong>en</strong>cia (&) es como un puntero constante que se de-refer<strong>en</strong>cia automáticam<strong>en</strong>te.<br />

Normalm<strong>en</strong>te se utiliza <strong>en</strong> la lista de argum<strong>en</strong>tos y <strong>en</strong> el valor de retorno de<br />

de las funciones. Pero también se puede hacer una refer<strong>en</strong>cia que apunte a algo que<br />

no ha sido asignado. Por ejemplo:<br />

//: C11:FreeStandingRefer<strong>en</strong>ces.cpp<br />

#include <br />

using namespace std;<br />

// Ordinary free-standing refer<strong>en</strong>ce:<br />

int y;<br />

int& r = y;<br />

// Wh<strong>en</strong> a refer<strong>en</strong>ce is created, it must<br />

// be initialized to a live object.<br />

// However, you can also say:<br />

const int& q = 12; // (1)<br />

// Refer<strong>en</strong>ces are tied to someone else’s storage:<br />

int x = 0; // (2)<br />

int& a = x; // (3)<br />

int main() {<br />

cout

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

Saved successfully!

Ooh no, something went wrong!