The Situation: I'm trying to implement two classes, one which is called 'special'. special
has a member variable bool conditions
and a method perform_special
.
The other class is named manager
and it has as a member variable of type special
. I want manager
to call perform_special
on its special
member only if condition
is true.
So far I have implemented this chunk of code:
#include<iostream>
using namespace std;
class special{
public:
special(){};
void perform_special();
void set_conditions( bool cond );
bool get_conditions();
private:
bool conditions;
};
void special::perform_special(){ cout<<"special performed."<<endl; }
void special::set_conditions( bool cond ){ conditions = cond; }
bool special::get_conditions(){ return conditions; }
class manager{
public:
manager(){};
void check_specials();
void set_effect( special* eff );
private:
special* effect;
};
void manager::check_specials(){
if(effect->get_conditions()){ effect->perform_special(); }
}
void manager::set_effect( special *eff ){ effect = eff; }
int main(){
int a=3; int b=2;
bool cond = a<b;
special effect1;
effect1.set_conditions( cond );
a=2; b=3;
manager manager1;
manager1.set_effect( &effect1 );
manager1.check_specials();
return 0;
}
This is what it does: It creates the boolean cond
which is immediately evaluated to be false
because at this point, a<b
is false. Now it gives this condition to a special variable which is again given to a manager variable. When the manager calls check_special
on its special variable, nothing happens because cond
is false.
here's my problem: I don't want cond
to be evaluated immediately. As you can see in the code, the values of a and be may change and thus the value of the expression a<b
. How can I achieve that the value of cond
is only evaluated when the function check_specials()
is called and using the latest values of the variables a and b?
Background: I'm trying to implement a text-based adventure-game, so 'special' would be some kind of special effect that happens if a bunch of conditions is true. 'manager' would be some kind of master class which handles the game and holds all the special effects etc. I want to declare those special effects in the main function and pass them to a variable 'game' of type manager
and then start the routine of the game so I need manager
to evaluate the conditional expressions for the special effects dynamically as the conditions obviously change over time.