fork download
  1. #include <iostream>
  2. #include <functional>
  3. #include <math.h>
  4. using namespace std;
  5. template<class VarType>class Proxy{
  6. VarType& Var;
  7. typedef const VarType& CVarRef;
  8. std::function<void(CVarRef)> Setter;
  9. std::function<CVarRef()> Getter;
  10. public:
  11. Proxy(VarType& Var,std::function<void(CVarRef Val)> Setter=nullptr,std::function<CVarRef()> Getter=nullptr):Var(Var),Setter(Setter),Getter(Getter){}
  12. ~Proxy(){}
  13. inline operator CVarRef()const{return Getter?Getter():Var;}
  14. inline const Proxy& operator=(CVarRef NewValue)const{
  15. if(Setter)Setter(NewValue);
  16. else Var=NewValue;
  17. return *this;
  18. }
  19. };
  20. class Hero{
  21. int _Exp;
  22. int _Level;
  23. public:
  24. // Variables.
  25. Proxy<int> Exp;
  26. Proxy<const int> Level;
  27. // Constructor.
  28. Hero(int StartingExp=0):_Exp(StartingExp),_Level(sqrt(StartingExp)),Exp(_Exp,bind(&Hero::SetExp,this,placeholders::_1)),Level(_Level){}
  29. // Destructor.
  30. ~Hero(){}
  31. // Functions.
  32. void SetExp(const int& NewExp){
  33. _Exp=NewExp;
  34. _Level=sqrt(NewExp);
  35. }
  36. // Function Pointers.
  37. // Operators.
  38. };
  39. ostream& operator<<(ostream& stream,const Hero& rhs){
  40. return stream<<"Hero of level "<<rhs.Level<<" with "<<rhs.Exp<<" exp.";
  41. }
  42.  
  43. int main(){
  44. Hero Bob;
  45. Bob.Exp=44;
  46. //Bob.Level=99;
  47. cout<<Bob<<endl;
  48. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
Hero of level 6 with 0 exp.