new_and_delete_hierarchy.carbon 847 B

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