fork download
  1. #include <iostream>
  2. #include <cassert>
  3. class Printer {
  4. static int count;
  5. public:
  6. static Printer* create() {
  7. return (count == 0) ? ++count, new Printer : 0;
  8. }
  9. ~Printer() {--count;}
  10. private:
  11. Printer() {}
  12. Printer(const Printer&);
  13. public:
  14. void print() {std::cout<<"I am a printer"<<std::endl;}
  15. };
  16.  
  17. int Printer::count = 0;
  18.  
  19. int main() {
  20. Printer* p1 = Printer::create();
  21. assert(p1);
  22. Printer* p2 = Printer::create();
  23. assert(!p2);
  24. delete p1;
  25. Printer* p3 = Printer::create();
  26. assert(p3);
  27. p3->print();
  28. Printer p4(*p3);
  29. delete p3;
  30. }
  31.  
Compilation error #stdin compilation error #stdout 0s 16064KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:28:19: error: ‘Printer::Printer(const Printer&)’ is private within this context
     Printer p4(*p3);
                   ^
prog.cpp:12:5: note: declared private here
     Printer(const Printer&);
     ^~~~~~~
stdout
Standard output is empty