C++, Adding conditions in class vars

2019-09-15 19:06发布

问题:

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).

In obj.h: (excluding preprocessor commands)

    class obj {
    public:
            void addCondition( bool cond );
            void useCondition();
    private:
            bool conditions;
    };

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...
            }
    }

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);

OR

    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.

Thanks in advance!

回答1:

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;
}

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);

sets conditions to false, and

obj.orCondition(true);

sets conditions to true


Edit per comments:

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 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;
}

Note : conditions is a list of pointers to boolean variables that can be re-evaluated

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 ...