virtual_destructor.carbon 1004 B

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