virtual_destructor.carbon 1020 B

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