fork download
  1. #include <cstdio>
  2. #include <functional>
  3. template <class T>
  4. class Property {
  5. T v;
  6. public:
  7. Property(const Property&p) {*this=static_cast<T>(p);}
  8. //Property() { printf("ctor %X\n", this);}
  9. Property(){}
  10. Property& operator=(const Property& src) { return*this=static_cast<T>(src); }
  11. Property& operator=(const T& src) {
  12. printf("write %X\n", this);
  13. v = src;
  14. return *this;
  15. }
  16. operator T() const {
  17. printf("Read %X\n", this);
  18. return v;
  19. }
  20. };
  21.  
  22. template <class T>
  23. class Property2 {
  24. typedef T(*Gfn)();
  25. typedef void(*Sfn)(T);
  26. Gfn gfn;
  27. Sfn sfn;
  28. public:
  29. Property2(const Property2&p) {*this=static_cast<T>(p);}
  30. //Property2() { printf("ctor %X\n", this);}
  31. Property2(Gfn gfn_, Sfn sfn_):gfn(gfn_), sfn(sfn_) {}
  32. Property2& operator=(const Property2& src) { return*this=static_cast<T>(src); }
  33. Property2& operator=(const T& src) {
  34. printf("write %X\n", this);
  35. sfn(src);
  36. return *this;
  37. }
  38. operator T() const {
  39. printf("Read %X\n", this);
  40. return gfn();
  41. }
  42. };
  43.  
  44. void set(int v) {}
  45. int get() {return 9;}
  46.  
  47. Property<int> a, b;
  48. Property2<int> c(get,set), d(get,set);
  49. int main(){
  50. a=b=5;
  51. c=d=11;
  52. a=c=b=d=15;
  53. }
  54.  
Success #stdin #stdout 0s 2828KB
stdin
Standard input is empty
stdout
write 8049890
Read 8049890
write 804988C
write 804989C
Read 804989C
write 8049894
write 804989C
Read 804989C
write 8049890
Read 8049890
write 8049894
Read 8049894
write 804988C