Skip to main content
gamedev.net
🔒 Locked

Function Pointers

Started by dawidjoubert Aug 7, 2006 at 7:08 AM 3 replies 700+ views
Original Post
dawidjoubert
dawidjoubert
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.

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;
	}

};


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?
----------------------------http://djoubert.co.uk
baldurk
baldurk
If you're using stl lists and vectors already, you might want to look at std::for_each. If it does what you want, it might be easier than trying to implement it all yourself. Failing that, I'm sure boost has a billion and one other ways to do this sort of thing.
dawidjoubert
dawidjoubert
Quote:
Original post by baldurk
If you're using stl lists and vectors already, you might want to look at std::for_each. If it does what you want, it might be easier than trying to implement it all yourself. Failing that, I'm sure boost has a billion and one other ways to do this sort of thing.


Solved then. Thanks
----------------------------http://djoubert.co.uk
snk_kid
snk_kid
Before using std::for_each every where, look at what other specialized generic algorithms are available for the task at hand first.

Quote:
Original post by dawidjoubert
I am sure using function pointers can solve this problem but i cant quite get it to work.


You mean you can't get your code to compile, that is because the type of your function pointer it's argument type is a pointer it should be a reference instead. Anyways you should be using the C++ standard library generic algorithms, they allow more than just accepting pointer to functions.

Quote:
Original post by dawidjoubert
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?


In this context (from your code) it will be:

#include <functional>#include <algorithm>....std::for_each(v.begin(), v.end(), std::mem_fun_ref(&CResource::Init));
Enigma
Enigma
Comments unrelated to the original problem, since that seems to have been solved:

Quote:
Original post by dawidjoubert
char filename[255];

Ugh!

Quote:
int CreateTexture(char *filename);

Double ugh! At least use a const char * so you can pass a std::string without having to jump through hoops!

Σnigma

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.