Double colon (::) in C is used for: 1. Global namespace access; 2. Namespace qualification; 3. Enum constant access; 4. Static method call; 5. Base class reference.
Double colon (::) in C
In C, double colon (::) operation The symbol has the following uses:
1. Global namespace access
For example:
<code class="cpp">::x = 10; // 访问全局变量 x</code>
2. Namespace qualification
For example:
<code class="cpp">namespace std { int a; } int main() { std::a = 10; // 访问 std 命名空间中的变量 a }</code>
3. Enumeration constant access
For example:
<code class="cpp">enum Color { Red, Green, Blue }; int main() { Color color = ::Color::Red; // 访问枚举常量 Red }</code>
4. Static method call
For example:
<code class="cpp">class MyClass { public: static void print() { cout << "Hello!" << endl; } }; int main() { MyClass::print(); // 调用静态方法 print }</code>
5. Base class reference
For example:
<code class="cpp">class Base { public: void print() { cout << "Base class" << endl; } }; class Derived : public Base { public: void print() { ::Base::print(); // 引用基类方法 print } };</code>
The above is the detailed content of What is :: in c language. For more information, please follow other related articles on the PHP Chinese website!