Double colon (::) is used for namespace scope resolution and class static member access, and single colon (:) is used for base class initialization and implicit type conversion.
The difference between :: and : in C
In the C programming language, double colon ( The ::) and single colon (:) operators have different usage and meaning.
Double colon(::)
Double colon(::) The operator is used in the following scenarios:
<code class="cpp">namespace outer { int x = 10; } namespace inner { void printX() { std::cout << outer::x << std::endl; } }</code>
<code class="cpp">class MyClass { public: static int numInstances = 0; static void printNumInstances() { std::cout << numInstances << std::endl; } };</code>
Single colon(:)
Single colon(:) The operator is used in the following scenarios:
<code class="cpp">class Base { public: int x; }; class Derived : public Base { public: Derived(int x) : x(x) {} };</code>
<code class="cpp">int x = 10; double y = static_cast<double>(x);</code>
Summary
Double colon (::) is used for namespace scope resolution and class static member access, while single colon (:) Used for base class initialization and implicit type conversion.
The above is the detailed content of The difference between :: and : in c++. For more information, please follow other related articles on the PHP Chinese website!