C ++,在类vars中添加条件 [英] C++, Adding conditions in class vars
问题描述
标题可能不正确,因为我找不到合适的词。
我想为某个对象(由类创建的实例)添加条件。
The title may not be right because I can't find an appropriate words for it. I want to add conditions to some object (instance created by class).
在obj.h中:(不包括预处理器命令)
In obj.h: (excluding preprocessor commands)
class obj {
public:
void addCondition( bool cond );
void useCondition();
private:
bool conditions;
};
在obj.cpp :(不包括预处理命令)
In obj.cpp: (excluding preprocessor commands)
void obj::addCondition( bool cond )
{
//What to write here, is the question in short!!!
}
void obj::useCondition()
{
if(conditions)
{
//Do something...
}
}
假设条件为:
conditions = value1> value2;
我想在条件下ADD一个条件,这样的话:
Suppose that the conditions was: conditions = value1 > value2; I wanted to 'ADD' a condition in conditions so, that it becomes something like that:
conditions = (value1 > value2) || (value3 <= value4);
或
conditions = (value 1 > value2) && (value3 <= value4);
如果我在问问题时发生错误,对不起!
If I am wrong about something in asking things, I am sorry! If you know something other than the answer but the whole different thing that does the same thing, don't hesitate to discuss it.
提前感谢!
推荐答案
我假设你知道为什么条件
字段和条件
参数都是简单的布尔变量。如果这是真的,可以很简单,但你应该用和Condition
和 addCondition
> orCondition ::
I assume you know why conditions
field and condition
parameter are both simple boolean variables. If that is true, it could be very simple, but you should replace addCondition
with andCondition
and orCondition
:
void obj::andCondition( bool cond )
{
conditions = conditions && condition;
}
void obj::orCondition( bool cond )
{
conditions = conditions || condition;
}
您应该定义条件是initialy true还是false。您可以随时将其设置为定义的值,因为上面的代码:
And you should define whether conditions is initialy true or false. You can always set it to a defined value, because with code above :
obj.andCondition(false);
将条件
设置为false, p>
sets conditions
to false, and
obj.orCondition(true);
将条件
设为true
根据评论进行编辑:
上述答案是基于以下要求: 条件
是一个简单的布尔变量,条件
是一个简单的布尔值。
The above answer is based on the requirement that conditions
is a simple boolean variable, and condition
is a simple boolean value.
这里是一个例子,如果你想重新评估条件可以做什么。
Here is one example of what could be done if you want to re-evaluate the condition.
A类和<使用 useCondition
时评估的布尔变量表示的(或or-ing)条件:
A class and-ing (resp. or-ing) conditions represented by boolean variables evaluated at the moment where useCondition
is used :
class andcond {
std::list<bool *> conditions;
public:
void addCondition(bool &condition) {
conditions.push_back(&condition);
}
bool isCondition();
};
bool andcond::isCondition() {
bool retval = true;
for (std::list<bool *>::iterator it = conditions.begin(); it != conditions.end(); it++) {
retval = retval && **it;
}
return retval;
}
int main() {
bool a=false, b=true;
andcond c;
c.addCondition(a);
c.addCondition(b);
std::cout << c.isCondition() << std::endl; // is false here
a = true;
std::cout << c.isCondition() << std::endl; // is true here
return 0;
}
注意: conditions
是可以重新求值的布尔变量的指针列表
Note : conditions
is a list of pointers to boolean variables that can be re-evaluated
你甚至可以通过定义一个完整的条件类层次实现一个 bool eval()
方法,例如2个变量之间的等式或不等式,并且可以通过和和
/ code>。但是,对于对SO的初步回答的厌恶,这太复杂了。但是你可以尝试实现这个想法,并在这里遇到困难时提出更精确的问题...
You could even be more genereral by defining a full hierarchy of condition classes implementing a bool eval()
method, for example the equality or inequality between 2 variables, and combinable by and
and or
. But it is way too complex for a disgression on an initial answer on SO. But you can try to implement this idea and ask more precise questions here when stuck ...
这篇关于C ++,在类vars中添加条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!