fork(1) download
  1. #include <iostream>
  2.  
  3. struct base {
  4. virtual ~base() {}
  5. virtual void func() = 0;
  6. };
  7.  
  8. struct derived : public base {
  9. derived() { std::cout << "Derived being created -- this = " << this << '\n'; }
  10. virtual void func() override { }
  11. ~derived() { std::cout << "Derived being destroyed -- this = " << this << '\n'; }
  12. };
  13.  
  14. struct base_haver {
  15. base_haver() : base_haver(derived{})
  16. {
  17. std::cout << "In the base_haver constructor body now\n";
  18. } // Provides default implementation
  19. base_haver(base && b) : mBase(b)
  20. {} // Caller provides implementation
  21.  
  22. void call_func() { mBase.func(); }
  23.  
  24. base & mBase;
  25. };
  26.  
  27. int main()
  28. {
  29. base_haver b;
  30. }
  31.  
Success #stdin #stdout 0s 4240KB
stdin
Standard input is empty
stdout
Derived being created -- this = 0x7ffff92e2a10
Derived being destroyed -- this = 0x7ffff92e2a10
In the base_haver constructor body now