我正在尝试创建一个尽可能高效的速度距离和时间计算器,并且想要使用根据输入而变化的指针来调用函数,但我不确定如何这样做.我尝试了很多不同的东西:
我最好的尝试:
// Inside class Math
double calcSpeed(double distance, double time); // These functions return a double value of the formula (non static)
double calcDistance(double speed, double time);
double calcTime(double speed, double distance);
// Inside Main
typedef double (Math::*FuncChosen)(double first, double second);
FuncChosen p = &Math::calcSpeed; // This changes according to input to different functions in Math class with same parameter types and return types
p(1, 2); // Not a function, how can I non explicitly call the function?
Run Code Online (Sandbox Code Playgroud)
有没有办法调用此函数,而无需使用指针或其他方式明确引用它.就像从根据输入改变的指针调用函数一样?我真的不知道从哪里开始和使用什么,因为我尝试的一切都是非法的.我基本上想在运行时选择该函数而不使用几个ifs,从而避免重复.(我发现有类似问题的人,但没有找到一种有效的方法来实现我的目的.) …