小编exi*_*ode的帖子

调用函数而不显式引用它根据输入而改变

我正在尝试创建一个尽可能高效的速度距离和时间计算器,并且想要使用根据输入而变化的指针来调用函数,但我不确定如何这样做.我尝试了很多不同的东西:

我最好的尝试:

// 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,从而避免重复.(我发现有类似问题的人,但没有找到一种有效的方法来实现我的目的.) …

c++ pointers typedef reference non-repetitive

2
推荐指数
1
解决办法
82
查看次数

如果没有if语句,确定变量是否小于0

有没有办法在不使用if语句的情况下确定变量是否小于0?

是的,这确实是一个非常奇怪的问题,但我仍然想知道.

c++

-5
推荐指数
1
解决办法
101
查看次数

标签 统计

c++ ×2

non-repetitive ×1

pointers ×1

reference ×1

typedef ×1