Telumehtar
asked on
A Query about Function Pointers
In my class 'galGeneticAlgorithm', I have the following definition for a function pointer:
bool (galGeneticAlgorithm::*gen erateChrom osomeData) (galGenome *);
Now, I would like to write a function in a completely seperate module, and then attach the function pointer in the class to the new function that I've made elsewhere. I have defined the function thusly, and naturally (it's simple) it compiles OK;
bool tempFunction (galGenome* g) { return true; }
However, a few lines later, I try to assign the function pointer in one instance of galGeneticAlgorithm. Here is the code I am using;
test = new galGeneticAlgorithm(1000);
test->generateChromosomeDa ta = &tempFunction;
Naturally, I get a horrid error telling me I can't do this. Here is said error;
error C2440: '=' : cannot convert from 'bool (__cdecl *)(galGenome *)' to 'bool (__thiscall galGeneticAlgorithm::* )(galGenome *)'
I understand that the function is defined as a 'standard function', and thus - isn't part of the correct class, but I don't know how to fix this. Please advise on how I can get this to be accepted, not only by Microsoft's Visual C++ .NET, but by all compilers (__thiscall is MSVC specific).
bool (galGeneticAlgorithm::*gen
Now, I would like to write a function in a completely seperate module, and then attach the function pointer in the class to the new function that I've made elsewhere. I have defined the function thusly, and naturally (it's simple) it compiles OK;
bool tempFunction (galGenome* g) { return true; }
However, a few lines later, I try to assign the function pointer in one instance of galGeneticAlgorithm. Here is the code I am using;
test = new galGeneticAlgorithm(1000);
test->generateChromosomeDa
Naturally, I get a horrid error telling me I can't do this. Here is said error;
error C2440: '=' : cannot convert from 'bool (__cdecl *)(galGenome *)' to 'bool (__thiscall galGeneticAlgorithm::* )(galGenome *)'
I understand that the function is defined as a 'standard function', and thus - isn't part of the correct class, but I don't know how to fix this. Please advise on how I can get this to be accepted, not only by Microsoft's Visual C++ .NET, but by all compilers (__thiscall is MSVC specific).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You could pass 'this' as a parameter to the global function - i.e. declare it like so:
bool tempFunction(galGenome*, galGeneticAlgorithm*);
bool tempFunction(galGenome*, galGeneticAlgorithm*);
ASKER
Of course - yup, that would work great.
ASKER
Just out of curiousities sake - is there any way that I could define a method, so that I could make use of the 'this' variable? I don't think I'll need to in this case, but there are a couple of places I can think of where it would be 'neat'.