fork download
  1. #ifdef _WIN32
  2. #include <windows.h>
  3. #pragma warning(disable:4355)
  4. #endif//_WIN32
  5.  
  6. #include <string>
  7. #include <iostream>
  8.  
  9.  
  10. struct Base;
  11. struct Widget;
  12.  
  13. struct VCons
  14. {
  15. VCons() : caller(this), base(NULL), param(NULL)
  16. {}
  17.  
  18. VCons(const VCons& vc) : caller(&vc), base(NULL), param(NULL)
  19. {}
  20.  
  21. ~VCons();
  22.  
  23. void Set(Base* c, Widget* p) const
  24. {
  25. base = c;
  26. param = p;
  27. }
  28.  
  29. private:
  30.  
  31. const VCons* caller;
  32. mutable Base* base;
  33. mutable Widget* param;
  34.  
  35. };
  36.  
  37.  
  38. struct Base
  39. {
  40. Base(Widget* p, VCons vc)
  41. {
  42. vc.Set(this, p);
  43. }
  44.  
  45. virtual void OnCreate(Widget* p)=0;
  46. };
  47.  
  48. struct Widget : public Base
  49. {
  50. Widget* parent;
  51.  
  52. Widget(Widget* p, VCons vc=VCons()) : Base(p, vc), parent(NULL)
  53. {}
  54.  
  55. void OnCreate(Widget* p)
  56. {
  57. parent = p;
  58. }
  59. };
  60.  
  61. struct Button : public Widget
  62. {
  63. Button(Widget* p, VCons vc=VCons()) : Widget(p, vc)
  64. {}
  65.  
  66. void OnCreate(Widget* p)
  67. {
  68. Widget::OnCreate(p);
  69. std::cout<<"Button"<<std::endl;
  70. }
  71. };
  72.  
  73. struct RadioButton : public Button
  74. {
  75. RadioButton(Widget* p, VCons vc=VCons()) : Button(p, vc)
  76. {}
  77.  
  78. void OnCreate(Widget* p)
  79. {
  80. Widget::OnCreate(p);
  81. std::cout<<"RadioButton"<<std::endl;
  82. }
  83. };
  84.  
  85. struct MainFrame : public Widget
  86. {
  87. Button button;
  88. RadioButton radio;
  89.  
  90. MainFrame(VCons vc=VCons()) : Widget(NULL, vc), button(this), radio(this)
  91. {}
  92.  
  93. void OnCreate(Widget* p)
  94. {
  95. Widget::OnCreate(NULL);
  96. std::cout<<"MainFrame"<<std::endl;
  97. }
  98. };
  99.  
  100. VCons::~VCons()
  101. {
  102. if(caller == this)
  103. base->OnCreate(param);
  104. else
  105. caller->Set(base, param);
  106. }
  107.  
  108. int main()
  109. {
  110. MainFrame frame;
  111.  
  112. int i=0;
  113. std::cin>>i;
  114. return 0;
  115. }
  116.  
  117.  
Success #stdin #stdout 0s 3304KB
stdin
Standard input is empty
stdout
Button
RadioButton
MainFrame