Help Needed With My Custom Iterator

closed account (zb0S216C)
I've created a custom test iterator structure that iterates through a pre-defined array for testing purposes. However, as with all projects, somethings never go right the first time. So, the iterator structure is very simple, and yet a simple problem eludes me. Here's the structure first of all:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int *Array( new int[ 3 ] );

struct Itor
{
	Itor( void )
	{
		Current = 0;
		EndIndex = 2;
	}

	int Current, EndIndex;
	
	int &GoForward( void )
	{
		if( ( Current + 1 ) > EndIndex )
			return Array[ ( Current = 0 ) ];             

		else
			return Array[ ( Current++ ) ];
	}

	int &GoBack( void )
	{
		if( ( Current - 1 ) < 0 )
			return Array[ ( Current = 0 ) ];

		else
			return Array[ ( Current-- ) ];
	}
};


In main( ), I call GoForward( ) 3 times. However, the first two calls print the first two values of Array as expected. Here's the problem: the third call supposed to print the third( final ) value in Array but it prints the first instead.

Right now I'm worn out, and I just want to find this problem before I fall asleep on the keyboard( I don't want an error saying: Undeclared identifier: 'ccccccccccccccccccccccccccccccccccccccccccccc' ).

As always, any help is appreciated.

Wazzak
The iterator gets constructed, current=0; end_index=2;
Calling GoForward current=0; end_index=2; -> current=1; end_index=2;
Calling GoForward current=1; end_index=2; -> current=2; end_index=2;
Calling GoForward current=2; end_index=2; -> 2+1>2 so current=0; end_index=2;
closed account (zb0S216C)
I figured it out. Thanks for you help, Ne555.

Wazzak
Topic archived. No new replies allowed.