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 285 — #323<br />

✐<br />

10.2. Espacios de nombres<br />

Integer a, b;<br />

Integer divide(Integer, Integer);<br />

// ...<br />

}<br />

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

También puede declarar todos los nombres <strong>en</strong> Int d<strong>en</strong>tro de la función pero<br />

dejando esos nombres anidados d<strong>en</strong>tro de la función:<br />

//: C10:Arithmetic.cpp<br />

#include "NamespaceInt.h"<br />

void arithmetic() {<br />

using namespace Int;<br />

Integer x;<br />

x.setSign(positive);<br />

}<br />

int main(){} ///:~<br />

Sin la directiva using, todos los nombres <strong>en</strong> el espacio de nombres requerirían<br />

estar completam<strong>en</strong>te explicitados.<br />

Hay un aspecto de la directiva using que podría parecer poco intuitivo al principio.<br />

La visibilidad de los nombres introducidos con una directiva using es el rango<br />

<strong>en</strong> el que se crea la directiva. Pero ¡puede hacer caso omiso de los nombres definidos<br />

<strong>en</strong> la directiva using como si estos hubies<strong>en</strong> sido declarados globalm<strong>en</strong>te para ese<br />

ámbito!<br />

//: C10:NamespaceOverriding1.cpp<br />

#include "NamespaceMath.h"<br />

int main() {<br />

using namespace Math;<br />

Integer a; // Hides Math::a;<br />

a.setSign(negative);<br />

// Now scope resolution is necessary<br />

// to select Math::a :<br />

Math::a.setSign(positive);<br />

} ///:~<br />

Suponga que ti<strong>en</strong>e un segundo espacio de nombres que conti<strong>en</strong>e algunos nombres<br />

<strong>en</strong> namespace Math:<br />

//: C10:NamespaceOverriding2.h<br />

#ifndef NAMESPACEOVERRIDING2_H<br />

#define NAMESPACEOVERRIDING2_H<br />

#include "NamespaceInt.h"<br />

namespace Calculation {<br />

using namespace Int;<br />

Integer divide(Integer, Integer);<br />

// ...<br />

}<br />

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

285<br />

✐<br />

✐<br />

✐<br />

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

Saved successfully!

Ooh no, something went wrong!