virtual_destructor.carbon 1005 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. base class B {
  13. extend base: A;
  14. impl destructor[self: Self] {
  15. Print("DESTRUCTOR B");
  16. }
  17. }
  18. class C {
  19. extend base: B;
  20. fn Create() -> Self{
  21. return {.base={.base={}}};
  22. }
  23. impl destructor[self: Self] {
  24. Print("DESTRUCTOR C");
  25. }
  26. }
  27. fn Main() -> i32 {
  28. Print("Allocate C");
  29. var pc: C* = heap.New(C.Create());
  30. var pa: A* = pc;
  31. Print("Delete C from A*");
  32. heap.Delete(pa);
  33. return 0;
  34. }
  35. // CHECK:STDOUT: Allocate C
  36. // CHECK:STDOUT: DESTRUCTOR C
  37. // CHECK:STDOUT: DESTRUCTOR B
  38. // CHECK:STDOUT: DESTRUCTOR A
  39. // CHECK:STDOUT: Delete C from A*
  40. // CHECK:STDOUT: DESTRUCTOR C
  41. // CHECK:STDOUT: DESTRUCTOR B
  42. // CHECK:STDOUT: DESTRUCTOR A
  43. // CHECK:STDOUT: result: 0