I have a series of basic functions; at certain points in my program, a parameter is checked and a corresponding function is called. I do this through a delegation function:
1 2 3 4 5 6
int calculate(X) {
switch(someParam) {
case 1: return function1(X);
case 2: return function2(X);
//...etc
}
This has always worked well, but now I find myself in a slightly different situation: rather than single calls, I need to update an entire array of several million elements, with the same function. Thus, by checking the parameter only once, I know which function to call in the entire process.
Obviously, rechecking through the delegation function is useless, but I can't find an elegant way around it. Anything I can think of either involves doubling up a lot of code, or redesigning my class layout.
Is there a way to "remember" a function name? I'm thinking something in line of binding a reference to a function, but I'm not sure that exists...
Thanks for the reply! Looks like exactly what I need!
Haven't gotten it to work yet, though. I think it might have something to do with the fact that it's a member function [so is the function that returns it], or that it's "const". The functions I want to return look like this one: inlinebool Solution::CGet(constint i, constint j) const { .. }
// Get correct V and C funcs.
CFunc C = CMove();
VFunc V = VMove();
// Using them
(this->*C)(i, j);
I'm getting this error:
>SMD.obj : error LNK2019: unresolved external symbol "public: int (__thiscall Solution::*__thiscall Solution::VMove(void)const )(unsigned int,unsigned int)const " (?VMove@Solution@@QBEP81@BEHII@ZXZ) referenced in function "public: void __thiscall Solution::SInit(void)" (?SInit@Solution@@QAEXXZ)
It's a bit sad, but I have no idea what the problem is. All functions involved are declared in the same .h file (inside the Solution class); the definitions are spread across different files, but they definitely exist.