virtual_method_shadowed_attr.carbon 812 B

12345678910111213141516171819202122232425262728293031323334353637
  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 {
  18. extend base: C;
  19. var value: i32;
  20. impl fn Foo[self: Self]() -> i32 {
  21. return self.value;
  22. }
  23. }
  24. fn Main() -> i32 {
  25. var c: C = {.value = 1};
  26. Print("c.Foo(): {0}", c.Foo());
  27. var d: D = {.value = 2, .base = {.value = 1}};
  28. Print("d.Foo(): {0}", d.Foo());
  29. var cp: C* = &d;
  30. Print("(*cp).Foo(): {0}", (*cp).Foo());
  31. return 0;
  32. }