fork(1) download
  1. #include <assert.h>
  2.  
  3. struct Parent {
  4. Parent(int number) : number_{number}{}
  5.  
  6. int number_{-1};
  7. };
  8. struct Child1 : private Parent { Child1() : Parent(1) {} };
  9. struct Child2 : private Parent { Child2() : Parent(2) {} };
  10.  
  11. void f(Child1& child1)
  12. {
  13. Parent& parent = child1;
  14. parent = Child2();
  15. }
  16.  
  17. int main()
  18. {
  19. Child1 child1;
  20. assert(child1.number_ == 1); // Ok
  21. f(child1);
  22. assert(child1.number_ == 2); // Ok :-) (Surprise for lamers) :-)
  23.  
  24. return 0;
  25. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'void f(Child1&)':
prog.cpp:13:20: error: 'Parent' is an inaccessible base of 'Child1'
   Parent& parent = child1;
                    ^
prog.cpp:14:10: error: 'Parent' is an inaccessible base of 'Child2'
   parent = Child2();
          ^
prog.cpp: In function 'int main()':
prog.cpp:6:17: error: 'int Parent::number_' is inaccessible
   int number_{-1};
                 ^
In file included from prog.cpp:1:0:
prog.cpp:20:17: error: within this context
   assert(child1.number_ == 1); // Ok
                 ^
prog.cpp:6:17: error: 'int Parent::number_' is inaccessible
   int number_{-1};
                 ^
In file included from prog.cpp:1:0:
prog.cpp:22:17: error: within this context
   assert(child1.number_ == 2); // Ok :-) (Surprise for lamers) :-)
                 ^
stdout
Standard output is empty