J
James Aguilar
Suppose I have a class Heap that represents a binary heap in an array. Then
suppose that I have another class called Heap::iterator that moves down the
heap to the left or the right. Here is the code from the header file
describing these two types:
--- CODE ---
class Heap
{
std::vector<int> m_vec;
public:
class iterator {
protected:
Heap *m_heap;
int m_position;
public:
enum Direction {LEFT, RIGHT};
iterator(Heap &myHeap, int position = 0);
iterator(const iterator &other);
int &operator *();
iterator left() const;
iterator right() const;
iterator child(const Direction &dir);
void walk(const Direction &dir);
};
Heap(int depth);
int &operator [](unsigned int index);
const int &operator [](unsigned int index) const;
iterator top();
void write(std:stream &o) const;
};
--- CODE ---
This is not compilable code. How would you recommend that I implement a
const_iterator while reusing code and not just copy and pasting method
bodies?
Thanks,
JFA1
suppose that I have another class called Heap::iterator that moves down the
heap to the left or the right. Here is the code from the header file
describing these two types:
--- CODE ---
class Heap
{
std::vector<int> m_vec;
public:
class iterator {
protected:
Heap *m_heap;
int m_position;
public:
enum Direction {LEFT, RIGHT};
iterator(Heap &myHeap, int position = 0);
iterator(const iterator &other);
int &operator *();
iterator left() const;
iterator right() const;
iterator child(const Direction &dir);
void walk(const Direction &dir);
};
Heap(int depth);
int &operator [](unsigned int index);
const int &operator [](unsigned int index) const;
iterator top();
void write(std:stream &o) const;
};
--- CODE ---
This is not compilable code. How would you recommend that I implement a
const_iterator while reusing code and not just copy and pasting method
bodies?
Thanks,
JFA1