重载运算符&&和||创建一个保存动态逻辑的类
我有一个从枚举创建的类,可以在将来的任何时候进行测试,返回一个布尔值是否通过,例如:
ConditionObject cnd(CondIsTuesday);
door.setOpenCondition(cnd);
然后门有一个可以用来确定其状态的条件。
不过我现在希望超载 &&和||此条件类的运算符,以便它们可以链接在一起并按照正常布尔逻辑预期的方式工作。例如,使以下情况成为可能:
ConditionObject cnd(ConditionObject(CondIsTuesday) || (ConditionObject(CondIsThursday) && ConditionObject(CondIsAfterEight)));
door.openCondition(cnd);
现在门将在周二或周四八点后打开。
我不确定描述问题的正确方法,这也使得很难寻找解决方案(如果已经存在)。感谢您对问题的任何帮助!
[编辑] 我认为我解释得不够好,我不希望在创建对象时评估 ConditionObject,而是将整个逻辑存储到最终的 ConditionObject 中,以便可以延迟评估它。
使用上面的第二个示例,如果我在星期一创建 cnd 对象并且从不给门另一个条件,那么它会在星期二打开,因为给定的条件开始返回 true。
I have a class that is created from an enum that can be tested at any point in the future returning a bool whether it passes, for example:
ConditionObject cnd(CondIsTuesday);
door.setOpenCondition(cnd);
Then the door has a condition that it can use to determine its state.
However I wish to now overload the && and || operators for this condition class such that they can be chained together and work in the way expected with normal boolean logic. For example, making the following possible:
ConditionObject cnd(ConditionObject(CondIsTuesday) || (ConditionObject(CondIsThursday) && ConditionObject(CondIsAfterEight)));
door.openCondition(cnd);
Now the the door will be open on tuesdays or thursdays after eight.
I wasn't sure of the correct way to describe the problem which also made it hard to search for a solution if one already existed. Thanks for any help with the problem!
[Edit]
I don't think I explained it well enough, I don't want the ConditionObject to be evaluated at the time of the creation of the object but instead to store the entire logic into the final ConditionObject such that it can be evaulated lazily.
Using the second example above, If I created the cnd object on a monday and never give the door anouther condition, it will open come tuesday because the condition it was given started returning true.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“表达式模板”可能对您有用。 这似乎是一个合理的彻底探讨这个主题,但还有其他的。我对这个主题不太熟悉,但这应该是一个很好的谷歌搜索点。
基本思想是,您想要一棵树,叶子是实际条件,叶子之间的分支是定义它们如何交互的运算符。表达式模板就是这棵树,您可以重载运算符来构建树。
然后,当您确实想知道门是否需要打开时,您可以沿着树走,依次检查每个条件,直到得到答案。短路评估(就像托尼提到的那样)应该是自然而然的,因为你最终会依靠内置的运算符。
"Expression Templates" might be of use to you. This seems to be a reasonably thorough take on the subject, but there are others. I'm not overly familiar with the subject, but that should be a good googling point.
The basic idea is that you want a tree, with the leafs being an actual condition, and the branches between them the operators that define how they interact. The expresion template is this tree, and you overload operators to build the tree.
Then, when you actually want to know if your door needs to open, you walk the tree, checking each condition in turn until you have your answer. Short-circuit evaluation [like Tony mentioned] should come naturally, since you ultimately fall back on the built in operators.
如果您超载 &&和||操作员,他们不能像正常人一样工作。例如:
对于正常的 &&运算符,如果左侧判断为 false,则不再评估右侧;但一旦你超载 &&运算符,该运算符将像 .=(A, B) 一样被调用,因此 A 和 B 都用作参数,它们都会被评估。
If you overload the && and || operators, they can't work like normal one. For example:
for normal && operator, if the left side is determined to false, the right side won't be evaluated any more; but once you overload the && operator, this operator will be invoked like .=(A, B), so both A and B are used as parameters, both of them will be evaluated.