virtual_method_shadowed_attr.carbon 804 B

123456789101112131415161718192021222324252627282930313233343536
  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: c.Foo(): 1
  7. // CHECK:STDOUT: d.Foo(): 2
  8. // CHECK:STDOUT: (*cp).Foo(): 2
  9. // CHECK:STDOUT: result: 0
  10. package ExplorerTest api;
  11. base class C {
  12. var value: i32;
  13. virtual fn Foo[self: Self]() -> i32 {
  14. return self.value;
  15. }
  16. }
  17. base class D extends C {
  18. var value: i32;
  19. impl fn Foo[self: Self]() -> i32 {
  20. return self.value;
  21. }
  22. }
  23. fn Main() -> i32 {
  24. var c: C = {.value = 1};
  25. Print("c.Foo(): {0}", c.Foo());
  26. var d: D = {.value = 2, .base = {.value = 1}};
  27. Print("d.Foo(): {0}", d.Foo());
  28. var cp: C* = &d;
  29. Print("(*cp).Foo(): {0}", (*cp).Foo());
  30. return 0;
  31. }