Pensar en C++ (Volumen 1) - Grupo ARCO
Pensar en C++ (Volumen 1) - Grupo ARCO
Pensar en C++ (Volumen 1) - Grupo ARCO
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 512 — #550<br />
✐<br />
Capítulo 16. Introducción a las Plantillas<br />
T* operator->() const {<br />
require(ps.storage[index] != 0,<br />
"PStash::iterator::operator->returns 0");<br />
return curr<strong>en</strong>t();<br />
}<br />
// Remove the curr<strong>en</strong>t elem<strong>en</strong>t:<br />
T* remove(){<br />
return ps.remove(index);<br />
}<br />
// Comparison tests for <strong>en</strong>d:<br />
bool operator==(const iterator& rv) const {<br />
return index == rv.index;<br />
}<br />
bool operator!=(const iterator& rv) const {<br />
return index != rv.index;<br />
}<br />
};<br />
iterator begin() { return iterator(*this); }<br />
iterator <strong>en</strong>d() { return iterator(*this, true);}<br />
};<br />
// Destruction of contained objects:<br />
template<br />
PStash::~PStash() {<br />
for(int i = 0; i < next; i++) {<br />
delete storage[i]; // Null pointers OK<br />
storage[i] = 0; // Just to be safe<br />
}<br />
delete []storage;<br />
}<br />
template<br />
int PStash::add(T* elem<strong>en</strong>t) {<br />
if(next >= quantity)<br />
inflate();<br />
storage[next++] = elem<strong>en</strong>t;<br />
return(next - 1); // Index number<br />
}<br />
template inline<br />
T* PStash::operator[](int index) const {<br />
require(index >= 0,<br />
"PStash::operator[] index negative");<br />
if(index >= next)<br />
return 0; // To indicate the <strong>en</strong>d<br />
require(storage[index] != 0,<br />
"PStash::operator[] returned null pointer");<br />
return storage[index];<br />
}<br />
template<br />
T* PStash::remove(int index) {<br />
// operator[] performs validity checks:<br />
T* v = operator[](index);<br />
// "Remove" the pointer:<br />
storage[index] = 0;<br />
return v;<br />
}<br />
512<br />
✐<br />
✐<br />
✐<br />
✐