06.08.2013 Views

Laboratory Exercises, C++ Programming

Laboratory Exercises, C++ Programming

Laboratory Exercises, C++ Programming

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.

<strong>Programming</strong> with the STL 33<br />

class BitsetIterator : public BitReference {<br />

public:<br />

// ... typedefs, see below<br />

};<br />

BitsetIterator(Bitset::BitsetStorage* pb, size_t p)<br />

: BitReference(pb, p) {}<br />

BitsetIterator& operator=(const BitsetIterator& bsi);<br />

bool operator!=(const BitsetIterator& bsi) const;<br />

BitsetIterator& operator++();<br />

BitReference operator*();<br />

The assignment operator must be redefined so it copies the member variables (as opposed to the<br />

assignment operator in the base class BitReference, which sets a bit in the bitset). The typedefs<br />

are necessary to make the iterator STL-compliant, i.e., to make it possible to use the iterator with<br />

the standard STL algorithms. The only typedef that isn’t obvious is:<br />

typedef std::forward_iterator_tag iterator_category;<br />

This is an example of an iterator tag, and it informs the compiler that the iterator is a forward<br />

iterator.<br />

A5. Implement the member functions in bitsetiterator.cc. Uncomment the lines in bitset.h and<br />

bitset.cc that have to do with iterators, implement the begin() and end() functions. Use<br />

the program bitsettest2.cc to test your classes.

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

Saved successfully!

Ooh no, something went wrong!