c++ - 避免一千个 if 语句的最佳方法?

标签 c++ boost rational-rose

我基本上遇到了这个问题:现在,我们有一个系统,它获取一个字符串作为输入,它基本上说 ACTION:。

对于每个 Action 都有一个自动生成的函数(Rational Rose GRRR),例如

bouncer_comm.chatMessage("data goes here").sendAt(msg->sapIndex0());
bouncer_comm.askforname().sendAt(msg->sapindex0());

bouncer_comm 返回一个 RTOutSignal,我无法手动创建它们,因为 rose 使用了奇怪的结构。

现在,我唯一的选择是创建一百个左右的 if 语句,我这样做:

if(action == "CHAT")  bouncer_comm.chatMessage("data goes here").sendAt(msg->sapIndex0());

这真的很烦人。

避免这种情况的最佳方法是什么?我看过/尝试过无数东西,这是一个非常老的 rational rose 版本(2k 之前),是的。

如果有人有任何想法,那就太棒了。

最佳答案

我喜欢@cobbal 上面的函数指针散列的想法,但是您可以用多态性替换这个条件逻辑。

参见:http://c2.com/cgi/wiki?ReplaceConditionalWithPolymorphism

关于c++ - 避免一千个 if 语句的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/670026/

相关文章:

c++ - 提升测试 'filename' .test.report.xml 未找到

c++ - Stringstream 的 Str 方法将不起作用。 (不同类型的串联)(C++)

c++ - Boost.MultiIndex 数据结构中的通配符搜索?

c++ - BGL : Is it possible to use range for of C++11 to loop over the vertices of the graph?

c++ - 如何在 STLport 库中使用 const auto

c++ - 如何自动获取 boost_major_version 号?

c++ - 为什么常量字符串在用于 variant<bool, std::string> 的输入时变成 bool?

methods - IBM Rational Rose : Is it possible to model class's operations and integrate it for code generation?

ibm-rational - IBM Rational 与 IBM Rational Rose 有何不同?