fork download
  1. #include <iostream>
  2.  
  3. template<class T ,class C,const T&(C::*Get0)()>
  4. class Getter{
  5. public:
  6.  
  7. Getter(C* c):Th(c){}
  8. //Getter() = delete;
  9. Getter(const Getter&) = delete;
  10.  
  11. operator const T&() {
  12. return (Th->*Get0)();
  13. }
  14.  
  15. const T& Get() {
  16. return (Th->*Get0)();
  17. }
  18.  
  19. protected:
  20. C *Th = nullptr;
  21. };
  22. template<class T ,class C,void (C::*Set0)(const T&)>
  23. class Setter{
  24. public:
  25.  
  26. Setter(C* c):Th(c){}
  27. //Setter() = delete;
  28. Setter(const Setter&) = delete;
  29.  
  30. Setter& operator =(const Setter& In) {
  31. (Th->*Set0)(In);
  32. return In;
  33. }
  34. const T& operator =(const T& In) {
  35. (Th->*Set0)(In);
  36. return In;
  37. }
  38. void Set(const T& In) {
  39. (Th->*Set0)(In);
  40. }
  41. protected:
  42. C *Th = nullptr;
  43. };
  44.  
  45. /**/
  46. template<class T, class C,const T& (C::*Get)(), void (C::*Set)(const T&), class TG = Getter<T,C,Get>, class TS = Setter<T,C,Set> >
  47. class Property : public TG, TS {
  48. public:
  49. Property(C* Th) : TG(Th), TS(Th) {}
  50. Property(const Property&) = delete;
  51. Property() = delete;
  52. /* Property& operator =(const Property& In) {
  53. (This->*Set(In));
  54. return In;
  55. }
  56. */
  57. };
  58. /**/
  59. template<class T>
  60. class Test {
  61. public:
  62.  
  63. typedef T Type;
  64.  
  65. Test():P(this),G(this),S(this){}
  66.  
  67. bool Show() {
  68. std::cout << N__ << std::endl;
  69. return true;
  70. }
  71.  
  72. protected:
  73. const T& TheGetter() {
  74. return N__;
  75. }
  76.  
  77. void TheSetter(const T& N_) {
  78. N__ = N_;
  79. }
  80.  
  81. protected:
  82. T N__ = 0;
  83. public:
  84. Property<T,Test<T>,&Test<T>::TheGetter,&Test<T>::TheSetter> P;
  85. Getter< T, Test<T>, &Test<T>::TheGetter> G;
  86. Setter < T, Test, &Test::TheSetter> S;
  87. };
  88.  
  89. int main() {
  90. Test<int> T;
  91. T.Show();
  92. T.S = 100;
  93. //int X = T.P;
  94. int Y = T.G;
  95.  
  96.  
  97. std::cout << Y << std::endl;
  98.  
  99. //T.P = 1000;
  100.  
  101. T.Show();
  102.  
  103. return 0;
  104. }
  105.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
0
100
100