virtual_destructor_nested.carbon 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. package ExplorerTest api;
  7. base class A {
  8. virtual destructor[self: Self] {
  9. Print("DESTRUCTOR A");
  10. }
  11. }
  12. class B {
  13. extend base: A;
  14. fn Create() -> Self{
  15. return {.base={}};
  16. }
  17. impl destructor[self: Self] {
  18. Print("DESTRUCTOR B");
  19. }
  20. }
  21. base class C {
  22. virtual destructor[self: Self] {
  23. Print("DESTRUCTOR C");
  24. }
  25. }
  26. class D {
  27. extend base: C;
  28. fn Create() -> Self{
  29. return {.base={}, .d_pa=heap.New(B.Create())};
  30. }
  31. impl destructor[self: Self] {
  32. Print("DESTRUCTOR D");
  33. }
  34. var d_pa: A*;
  35. }
  36. fn Main() -> i32 {
  37. Print("Allocate D");
  38. var pd: D* = heap.New(D.Create());
  39. var pc: C* = pd;
  40. Print("Delete B from A*");
  41. heap.Delete(pd->d_pa);
  42. Print("Delete D from C*");
  43. heap.Delete(pc);
  44. return 0;
  45. }
  46. // CHECK:STDOUT: Allocate D
  47. // CHECK:STDOUT: DESTRUCTOR B
  48. // CHECK:STDOUT: DESTRUCTOR A
  49. // CHECK:STDOUT: DESTRUCTOR D
  50. // CHECK:STDOUT: DESTRUCTOR C
  51. // CHECK:STDOUT: Delete B from A*
  52. // CHECK:STDOUT: DESTRUCTOR B
  53. // CHECK:STDOUT: DESTRUCTOR A
  54. // CHECK:STDOUT: Delete D from C*
  55. // CHECK:STDOUT: DESTRUCTOR D
  56. // CHECK:STDOUT: DESTRUCTOR C
  57. // CHECK:STDOUT: result: 0