fork download
  1. #include <iostream>
  2.  
  3. void link_test_fail(const int &x) // ошибка, только если передача по ссылке
  4. {
  5. std::cout << x << std::endl;
  6. }
  7.  
  8. void link_test_ok(int x)
  9. {
  10. std::cout << x << std::endl;
  11. }
  12.  
  13. class Foo
  14. {
  15. public:
  16. static const int i; // ошибка только с константным полем
  17. static int k;
  18.  
  19. void bar();
  20. };
  21.  
  22. int Foo::k = 12;
  23. const int Foo::i = 42;
  24.  
  25. void Foo::bar()
  26. {
  27. link_test_fail(i);
  28. }
  29.  
  30. int main() {
  31. Foo foo;
  32. foo.bar(); // ошибка линковки
  33. link_test_fail(Foo::i); // ошибка линковки
  34.  
  35. link_test_fail(Foo::k); // ok
  36. link_test_ok(Foo::i); // ok
  37. std::cout << Foo::i << std::endl; // ок
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0.01s 5548KB
stdin
Standard input is empty
stdout
42
42
12
42
42