Doppelter Doppelpunkt (::) wird in C++ verwendet für: 1. Globaler Namespace-Zugriff;
Doppelter Doppelpunkt (::) in C++
In C++ hat der Doppelpunkt (::)-Operator die folgenden Verwendungszwecke:
1. Globaler Namespace-Zugriff
Zum Beispiel:
::x = 10; // 访问全局变量 x
2. Namespace-Qualifikation
Zum Beispiel:
namespace std { int a; } int main() { std::a = 10; // 访问 std 命名空间中的变量 a }
3. Aufzählungskonstantenzugriff
Zum Beispiel:
enum Color { Red, Green, Blue }; int main() { Color color = ::Color::Red; // 访问枚举常量 Red }
4. Statischer Methodenaufruf
Zum Beispiel:
class MyClass { public: static void print() { cout << "Hello!" << endl; } }; int main() { MyClass::print(); // 调用静态方法 print }
5. Basisklassenreferenz
Beispiel:
class Base { public: void print() { cout << "Base class" << endl; } }; class Derived : public Base { public: void print() { ::Base::print(); // 引用基类方法 print } };
Das obige ist der detaillierte Inhalt vonWas ist :: in C-Sprache?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!