fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <memory>
  4.  
  5. struct Other
  6. {
  7. virtual void print() = 0;
  8. virtual ~Other() = default;
  9. };
  10.  
  11. struct Base
  12. {
  13. Base(Other *o)
  14. {
  15. o->print();
  16. }
  17. virtual ~Base() = default;
  18. };
  19. struct Derived : Base
  20. {
  21. Derived();
  22. virtual ~Derived();
  23. private:
  24. struct My;
  25. std::unique_ptr<My> my;
  26. My *temp;
  27. };
  28.  
  29. int main()
  30. {
  31. Derived d;
  32. }
  33.  
  34. struct Derived::My : Other
  35. {
  36. std::string s = "It works";
  37. virtual void print()
  38. {
  39. std::cout << s << std::endl;
  40. }
  41. };
  42. Derived::Derived()
  43. : Base(temp = new My)
  44. , my(temp)
  45. {
  46. temp = nullptr;
  47. }
  48. Derived::~Derived() = default;
  49.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
It works