new_and_delete_hierarchy.carbon 858 B

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