【发布时间】:2012-05-12 12:56:55
【问题描述】:
如何将基本算术运算符存储在变量中?
我想在 c++ 中做这样的事情:
int a = 1;
int b = 2;
operator op = +;
int c = a op b;
if (c == 3) // do something
由于我只考虑+、-、* 和/,我可以将运算符存储在string 中,然后只使用switch 语句。但是我想知道是否有更好/更简单的方法。
【问题讨论】:
-
查看 STL 函数如何接受谓词。你可以使用
std::plus之类的东西。 -
enum是一个选项吗?字符串具体有什么问题/
标签: c++ operators switch-statement arithmetic-expressions