fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Countable {
  5. int count;
  6.  
  7. incr() { count++; }
  8. };
  9.  
  10. struct Parent {
  11. static int count;
  12.  
  13. Parent() {
  14. count++;
  15. }
  16. };
  17.  
  18. int Parent::count = 0;
  19.  
  20. struct Child1: public Parent {
  21. Child1(): Parent(), cnt() { incr(); }
  22. static Countable cnt;
  23. };
  24.  
  25. int Child1::count = 0;
  26.  
  27. struct Child2: public Parent {
  28. Child2(): Parent() {}
  29. static int count;
  30. };
  31.  
  32. int Child2::count = 0;
  33.  
  34. int main() {
  35. Child1 ch1;
  36. Child1 ch2;
  37. Child1 ch3;
  38. Child2 ch22;
  39. Child2 ch23;
  40.  
  41. cout << Child1::count << endl;
  42.  
  43. return 0;
  44. }
Compilation error #stdin compilation error #stdout 0s 4256KB
stdin
Standard input is empty
compilation info
prog.cpp:7:8: error: ISO C++ forbids declaration of ‘incr’ with no type [-fpermissive]
   incr() { count++; }
        ^
prog.cpp: In constructor ‘Child1::Child1()’:
prog.cpp:21:22: error: ‘Countable Child1::cnt’ is a static data member; it can only be initialized at its definition
  Child1(): Parent(), cnt() { incr(); }
                      ^~~
prog.cpp:21:35: error: ‘incr’ was not declared in this scope
  Child1(): Parent(), cnt() { incr(); }
                                   ^
prog.cpp: At global scope:
prog.cpp:25:13: error: ISO C++ does not permit ‘Parent::count’ to be defined as ‘Child1::count’ [-fpermissive]
 int Child1::count = 0;
             ^~~~~
prog.cpp:25:13: error: redefinition of ‘int Parent::count’
prog.cpp:18:5: note: ‘int Parent::count’ previously defined here
 int Parent::count = 0;
     ^~~~~~
stdout
Standard output is empty