# 范围解析运算符：::

``````:: identifier class-name :: identifier namespace :: identifier enum class :: identifier enum struct :: identifier
``````

## 备注

identifier 可以是变量、函数或枚举值。

## 具有命名空间和类

``````namespace NamespaceA{
int x;
class ClassA {
public:
int x;
};
}

int main() {

// A namespace name used to disambiguate
NamespaceA::x = 1;

// A class name used to disambiguate
NamespaceA::ClassA a1;
a1.x = 2;

}
``````

``````namespace NamespaceA{
int x;
}

int x;

int main() {
int x;

// the x in main()
x = 0;
// The x in the global namespace
::x = 1;

// The x in the A namespace
NamespaceA::x = 2;
}
``````

``````namespace NamespaceB {
class ClassB {
public:
int x;
};
}

namespace NamespaceC{
using namespace B;

}
int main() {
NamespaceB::ClassB c_b;
NamespaceC::ClassB c_c;

c_b.x = 3;
c_c.x = 4;
}
``````

``````namespace NamespaceD{
namespace NamespaceD1{
int x;
}
}

namespace NamespaceE{

class ClassE{
public:
class ClassE1{
public:
int x;
};
};
}

int main() {
NamespaceD:: NamespaceD1::x = 6;
NamespaceE::ClassE::ClassE1 e1;
e1.x = 7  ;
}
``````

## 具有静态成员

``````class ClassG {
public:
static int get_x() { return x;}
static int x;
};

int ClassG::x = 6;

int main() {

int gx1 = ClassG::x;
int gx2 = ClassG::get_x();
}
``````

## 具有区分范围的枚举

``````enum class EnumA{
First,
Second,
Third
};

int main() {

EnumA enum_value = EnumA::First;
}
``````

C++ 运算符优先级和关联性