J
James Gregory
I was having some issues with a program involving iterators and handles,
and decided to try and make a short test program to try to work out where
my problem was. However, my test program went wrong long before it
resembled anything like my actual program.
Here is the short test program:
#include <iostream>
#include <list>
using std::cout;
using std::endl;
using std::list;
int globalCounter = 0;
class MyClass
{
public:
MyClass() {classCounter = globalCounter++;}
int classCounter;
};
class MyHandle
{
public:
MyHandle() {myPointer = new MyClass; handleCounter = myPointer->classCounter;}
~MyHandle() {delete myPointer;}
int GetCounter() {return myPointer->classCounter;}
int handleCounter;
private:
MyClass* myPointer;
};
int main()
{
list<MyHandle> myList;
for (int i = 0; i != 10; ++i)
myList.push_back(MyHandle());
for (list<MyHandle>::iterator iter = myList.begin(); iter != myList.end(); ++iter)
cout << iter->handleCounter << " " << iter->GetCounter() << endl;
return 0;
}
The output I get is:
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
But I was expecting:
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
Where have I gone wrong?
Thanks,
James
and decided to try and make a short test program to try to work out where
my problem was. However, my test program went wrong long before it
resembled anything like my actual program.
Here is the short test program:
#include <iostream>
#include <list>
using std::cout;
using std::endl;
using std::list;
int globalCounter = 0;
class MyClass
{
public:
MyClass() {classCounter = globalCounter++;}
int classCounter;
};
class MyHandle
{
public:
MyHandle() {myPointer = new MyClass; handleCounter = myPointer->classCounter;}
~MyHandle() {delete myPointer;}
int GetCounter() {return myPointer->classCounter;}
int handleCounter;
private:
MyClass* myPointer;
};
int main()
{
list<MyHandle> myList;
for (int i = 0; i != 10; ++i)
myList.push_back(MyHandle());
for (list<MyHandle>::iterator iter = myList.begin(); iter != myList.end(); ++iter)
cout << iter->handleCounter << " " << iter->GetCounter() << endl;
return 0;
}
The output I get is:
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
But I was expecting:
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
Where have I gone wrong?
Thanks,
James