fork(1) download
  1. #include <iostream>
  2. #include <memory>
  3. using namespace std;
  4.  
  5. struct IParse{
  6. virtual void foo() = 0;
  7. };
  8.  
  9. struct P1 : IParse{
  10. void foo(){
  11. cout<<"Hi1"<<endl;
  12. }
  13. };
  14.  
  15. struct P2 : IParse {
  16. void foo(){
  17. cout<<"Hi2"<<endl;
  18. }
  19. };
  20.  
  21. unique_ptr<IParse> getParser(int x){
  22. switch (x){
  23. case 1:
  24. return unique_ptr<IParse>(new P1());
  25. break;
  26. case 2:
  27. return unique_ptr<IParse>(new P2());
  28. break;
  29. }
  30. return nullptr;
  31. }
  32.  
  33. int main() {
  34. auto p1(getParser(1));
  35. p1->foo();
  36.  
  37. auto p2(getParser(2));
  38. p2->foo();
  39. return 0;
  40. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
Hi1
Hi2