virtual_destructor_nested.carbon 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // AUTOUPDATE
  6. // CHECK:STDOUT: Allocate D
  7. // CHECK:STDOUT: DESTRUCTOR B
  8. // CHECK:STDOUT: DESTRUCTOR A
  9. // CHECK:STDOUT: DESTRUCTOR D
  10. // CHECK:STDOUT: DESTRUCTOR C
  11. // CHECK:STDOUT: Delete B from A*
  12. // CHECK:STDOUT: DESTRUCTOR B
  13. // CHECK:STDOUT: DESTRUCTOR A
  14. // CHECK:STDOUT: Delete D from C*
  15. // CHECK:STDOUT: DESTRUCTOR D
  16. // CHECK:STDOUT: DESTRUCTOR C
  17. // CHECK:STDOUT: result: 0
  18. package ExplorerTest api;
  19. base class A {
  20. virtual destructor[self: Self] {
  21. Print("DESTRUCTOR A");
  22. }
  23. }
  24. class B extends A {
  25. fn Create() -> Self{
  26. return {.base={}};
  27. }
  28. impl destructor[self: Self] {
  29. Print("DESTRUCTOR B");
  30. }
  31. }
  32. base class C {
  33. virtual destructor[self: Self] {
  34. Print("DESTRUCTOR C");
  35. }
  36. }
  37. class D extends C {
  38. fn Create() -> Self{
  39. return {.base={}, .d_pa=heap.New(B.Create())};
  40. }
  41. impl destructor[self: Self] {
  42. Print("DESTRUCTOR D");
  43. }
  44. var d_pa: A*;
  45. }
  46. fn Main() -> i32 {
  47. Print("Allocate D");
  48. var pd: D* = heap.New(D.Create());
  49. var pc: C* = pd;
  50. Print("Delete B from A*");
  51. heap.Delete(pd->d_pa);
  52. Print("Delete D from C*");
  53. heap.Delete(pc);
  54. return 0;
  55. }