Re: TCPL
On 10=D4 4=C8=D5, =CF =CE=E710=CA=B152=B7=D6, Pete Becker <p...@versati=
lecoding.com> wrote:
On 2008-10-04 09:03:30 -0400, "bashill....@gmail.com"
<bashill....@gmail.com> said:
Ptr_to_T& operator++(int){//postfix
cout << "Entering Operator++" << endl;
T* temp = _p;
_p += 1;
return *this;
}
This operator increments *this then returns it. What it should do is
make a local copy of *this, increment *this, and return the local copy
by value. Typically, that looks like this:
my_iterator my_iterator::operator++(int)
{
my_iterator temp(*this);
++*this;
return temp;
}
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
Thanks all, Got it!
#include <iostream>
using namespace std;
template<typename T>
class Ptr_to_T
{
public:
class Range{};
Ptr_to_T(T* p, T* array, int size):_p(p),_array(array),_size(size)
{
}
Ptr_to_T(T* p):_p(p){}
Ptr_to_T& operator++(){//prefix
_p += 1;
return *this;
}
const Ptr_to_T operator++(int){//postfix
T* temp = _p;
_p += 1;
return Ptr_to_T(temp, temp, _array + _size - temp);
}
T& operator*(){
check();
return *_p;
}
private:
void check(){
if( _p - _array >= _size || _p < _array){
cout << _p - _array << endl;
throw Range();
}
}
T* _p;
T* _array;
int _size;
};