fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class B {
  5. public:
  6. B() {
  7. // complex initializations
  8. };
  9. virtual int doSomething()
  10. {return 2 * doSomethingHelper();}
  11.  
  12. protected:
  13. class initialiser {
  14. public:
  15. initialiser(B*b) {b->init();}
  16. };
  17. void init() { // the rest of the what you wanted in constructor
  18. int a = doSomething();
  19. // more complex stuff with `a`
  20. }
  21. virtual int doSomethingHelper() = 0;
  22. };
  23.  
  24.  
  25. class C: public B {
  26. public:
  27. protected:
  28. B::initialiser bi{this}; // this truggers automaticcaly the second phase
  29. virtual int doSomethingHelper()
  30. {cout <<"OK"<<endl; return 1;}
  31. };
  32.  
  33. int main() {
  34. C c;
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
OK