Modify the Vector class to provide stringent iterator checking by making iterators class types rather than pointer variables. The hardest part is dealing with stale iterators, as described in Exercise.
Exercise
According to the C++ standard, for the vector, a call to push_back, pop_back, insert, or erase invalidates (potentially makes stale) all iterators viewing the vector. Why?
Modified Vector class elements
The modified subclasses of the vector class to provide the stringent iterator check options are as given below:
The class const_iterator maintains the pointer to the current element in the class vector.
// class to maintain iterator
class const_iterator
{
// public member functions
public:
The default constructor assigns value null to the current pointer.
// constructor to assign null
const_iterator()
: current(NULL)
{}
The operator * is overloaded and returns the value received from the retrieve method.
// overload the operator *
const Object & operator*() const
{
// return the value received by the retrieve method
return retrieve();
}
Overload the increment operator which returns the pointer to the new increased address location.
// overload the increment operator
const_iterator & operator++()
{
// move the current poitner to the next address
current++;
// return current pointer
return *this;
}
Overload the increment operator to return the increased value at the pointer location.
// overload the increment operator
const_iterator operator++()
{
// store the previous location
const_iterator old = *this;
// increment the pointer
++(*this);
// return the value
return old;
}
The function which overloads the compare operator returns boolean value.
// overload the compare operator
bool operator == (const const_iterator & rhs) const
{
// return the boolean value
return current == rhs.current;
}
The function which overloads the not equal to operator returns boolean value.
// overload the not equal operator
bool operator != (const const_iterator &rhs) const
{
// return the boolean value
return !(*this == rhs);
}
The protected members of the class are declared. The Vector pointer which contains the address of type Object is declared.
protected:
// declare pointer of the Object class
Object *current;
// declare the vector pointer
const Vector
The constructor is overloaded, with vector of objects as argument.
// overload the constructor passing vector pointer to it
const_iterator(const Vector
The assertIsValid function checks if the current pointer is assigned a value or vector pointer contains a value, both are not null.
// the function assertIsValid
void assertIsValid() const
{
// check if the vector to the current pointer is null or vector is null
if(vectorPointer == NULL || current == NULL)
{
// raise an exception
throw IteratorOutOfBoundsException();
}
}
// declare the friend class
friend class Vector
The class iterator maintains the pointer to the current element.
// class iterator
class iterator : public const_iterator
{
// public members of the class
public:
The default constructor.
// default constructor
iterator()
{}
The operator * is overloaded and returns the value received from the retrieve method.
// overload the multiplication operator
Object & operator *()
{
// return value
return retrieve();
}
The operator * is overloaded and returns the value received from the retrieve method.
// overload the multiplcation operator
const Object & operator*() const
{
// return the value by calling the operator function from the const_iterator class
return const_iterator::operator*();
}
Overload the increment operator to return the increased value at the pointer location.
// overload the increment operator
iterator & operator++()
{
// display the value at the current pointer
cout<<"old = "<< *current<<" ";
// increment the pointer
current++;
// dislplay the value now with the current pointer
cout<<"new = "<<*current<<" ";
// return current pointer
return *this;
}
Overload the increment operator to return the increased value at the pointer location.
// overload the increment operator
iterator operator++()
{
// store current pointer to old
iterator old = *this;
// incremtne the current pointer value
++(*this);
// return the pointer.
return old;
}
protected:
The constructor is overloaded, with vector of objects as argument.
// overload the constructor using vector argument
iterator(const Vector
The begin function which returns the iterator object.
// define the function begin
iterator begin()
{
// return the value
return iterator(*this, &Objects[0]);
}
The begin function which returns the const_iterator object.
// defind the function begin for the const_iterator
const_iterator begin() const
{
// reutrn the value
return const_iterator(*this, &Object[0]);
}
The end function which returns iterator object.
// defind the function end for the iterator class
iterator end()
{
// return the value
return iterator(*this, &Object[ size() ]);
}
The end function which returns const_iterator object.
// define the function end for the iterator class
const_iterator end() const
{
// return the value.
return const_iterator(*this, &Object[ size() ]);
}