Hi, firstly I will show an example application that represents my issue:
I want findCell to return a pointer to the cell. Firstly I have to say that iterators confuse me a bit, they seem to be neither one thing nor the other,
in the loop they work just like a pointer, but I can't return the iterator as the pointer, and I can't type cast it (tried a few other things untill I landed on the code above).
I really don't want to be passing object data around everywhere as that would be inefficient.
What is the correct solution? Or should I find a different way to do the same task?
edit:
solved
Got some help on gamedev.net
Turns out I need to use:
return &(*cellI);
Also, I would be better off using a vector than a list, because vectors are faster for random access.
Code:
class cell{
public:
float top,left,bottom,right;
//other methods and members
};
list<cell> cellList;
list<cell>::iterator cellIter;
//finds the cell that a point is in and returns a pointer to the cell
cell* findCell(float x,float y){
for(cellIter = cellList.begin();cellIter != cellList.end();cellIter++){
//if point is inside the cell
if(x<=cellIter->right && x>=cellIter->left && y<=cellIter->top && y>=cellIter->bottom){
break; //break the loop
}
}
return (*cellIter);
}
I want findCell to return a pointer to the cell. Firstly I have to say that iterators confuse me a bit, they seem to be neither one thing nor the other,
in the loop they work just like a pointer, but I can't return the iterator as the pointer, and I can't type cast it (tried a few other things untill I landed on the code above).
I really don't want to be passing object data around everywhere as that would be inefficient.
What is the correct solution? Or should I find a different way to do the same task?
edit:
solved
Got some help on gamedev.net
Turns out I need to use:
return &(*cellI);
Also, I would be better off using a vector than a list, because vectors are faster for random access.
Last edited: