首页 > 解决方案 > 查找名称的过程描述

问题描述

帮助描述在片段的最后一行找到名称 x 的过程:

struct S { static int x; void f(); };
int S::x;
int x;
void S::f() { x; // <-- }

标签: c++

解决方案


我不确定我是否理解您的问题,但如果您想访问全局变量x而不是成员x,您可以使用scope resolution operator ::

void S::f() {
  // Assign value of global variable to member x
  x = ::x;

  // Use the value of the member x
  int y = x;
}

推荐阅读