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