struct_with_static_member

PHOTO EMBED

Fri Mar 11 2022 09:17:11 GMT+0000 (Coordinated Universal Time)

Saved by @gtsekas #c++

#include<iostream>

int foo()
{
  return 10;
}

struct foobar
{
  static int x;
  static int foo()
  {
    return 11;
  }
};

int foobar::x = foo();

int main()
{
    std::cout << foobar::x;
}
content_copyCOPY

Answer The output is: 11 Explanation: §[basic.lookup.unqual]¶13 states "A name used in the definition of a static data member of class X (...) is looked up as if the name was used in a member function of X." Even though the call foo() occurs outside the class, since foo is used in the definition of the static data member foobar::x, it is looked up as if foo() was called in a member function of foobar. If foo() was called in a member function of foobar, foobar::foo() would be called, not the global foo().

https://cppquiz.org/