In C, the :: operator is used to access static members or global variables of a class. It lets you access static members even without a class instance, and global variables even without a source file.
:: Usage in C
:: is a operator, called the domain resolution operator. It is used to access static members and global variables of a class.
Accessing static members of a class
Static members belong to the class rather than the class instance. You can use the :: operator to access static members of a class even if there is no instance of the class.
<code class="cpp">class MyClass { public: static int numInstances; }; int main() { // 访问静态成员 cout << MyClass::numInstances << endl; return 0; }</code>
Accessing global variables
Global variables are available in all code files. You can use the :: operator to access global variables even if there is no source file in which they are defined.
<code class="cpp">// 定义全局变量 int globalVar = 10; // 在另一个文件中访问全局变量 int main() { cout << ::globalVar << endl; return 0; }</code>
Note:
The above is the detailed content of In c++::how to use. For more information, please follow other related articles on the PHP Chinese website!