Original Post
Hey im using a lot of lists and vectors, now everytime i want to apply a function to every element i need to rewrite the traverse function in c++. I am sure using function pointers can solve this problem but i cant quite get it to work. Further say I have a function like CResource.Init() how can I use pointer functions to iterate the entire list and execute each resoure's specific init function?
struct CResource
{
char filename[255]; // Whether texture or shader this is the filename
int TextureCount;
int TextureID[8]; // Support for upto 8 multiple textures per shader...
bool TextureLoaded;
};
// Singleton class
class CGlobalTextures
{
public:
vector<CResource> resourceList;
vector<CResource>::iterator resourceIterator;
// Constructor
CGlobalTextures()
{
resourceList.clear();
}
// Destructor
~CGlobalTextures()
{
resourceList.clear();
}
int CreateTexture(char *filename); // Creates the texture and returns a unique resource for it
void Release(int id) {return; }; // If no objects require the texture in question any more it is freed
void Bind(int id) {return; }; // adds another object requiring the texture in question
// Returns true only if all iterations returned true
bool IterateExecuting(bool (*pt2Func)(*CResource))
{
bool result = true;
for (resourceIterator = resourceList.begin();resourceIterator != resourceList.end();resourceIterator++)
{
if (pt2Func((*resourceIterator)) == false) result = false;
}
return result;
}
};