virtual_method_shadowed_attr.carbon 813 B

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