The meaning and function of double colon (::) and single colon (:) in C++

Table of contents

1. The meaning and function of double colon (::) in C++

2. The meaning and function of single colon (:) in C++


Both double colon (::) and single colon (:) are special symbols in C++, and they have different meanings and effects.

1. The meaning and function of double colon (::) in C++

The double colon (::) is a scope parser in C++, which is used to indicate the scope of variables, functions or classes in the class scope. The left side of the double colon can be a class, namespace or enumeration type, and the right side can be a static member of a class, a member of a namespace or a global variable/function. Use double colons to specify the use of global variables or functions within the scope of the class, or specify to call members of other namespaces or parent classes, as in the following example:

namespace MyNamespace {
    int myInt = 1;
    void myFunction() {}
}

class MyClass {
public:
    static int myStaticInt;
    void myMethod() {
        // 使用双冒号指定调用 MyNamespace 命名空间的 myFunction 函数
        MyNamespace::myFunction();
        // 使用双冒号指定访问 MyNamespace 命名空间的 myInt 变量
        int x = MyNamespace::myInt;
    }
};

// 使用双冒号指定定义 MyClass 的静态成员 myStaticInt
int MyClass::myStaticInt = 0;

// 使用双冒号指定访问 MyClass 的静态成员 myStaticInt
int x = MyClass::myStaticInt;

        1. Namespace scope resolves symbols. When you need to use a member in the namespace, you can use double colons to indicate the namespace in which the member is located, as follows:

namespace my_namespace {
    int my_variable = 123;
}

// 在其他地方使用 my_namespace 中的 my_variable
std::cout << my_namespace::my_variable << std::endl;

In the above code, my_namespace::my_variablea double colon is used to indicate a member my_variableof my_namespacethe namespace.

        2. Indicates scope qualifiers. When you need to use an external global variable or function in the definition of a class, you can use double colons to indicate the scope of the variable or function, as shown below:

int a = 123; // 全局变量

class MyClass {
public:
    void my_method() {
        std::cout << "a = " << a << std::endl; // 错误,a 不在 MyClass 的作用域中
        std::cout << "a = " << ::a << std::endl; // 正确,使用全局作用域中的 a
    }
};

In the code above, ::adouble colons are used to denote avariables in the global scope.

        3. Qualifiers for nested classes or namespaces. When you need to use a member in a nested class or namespace, you can use double colons to indicate the nesting level of the member, as shown below:

class MyClass {
public:
    class NestedClass {
    public:
        void nested_method() {
            std::cout << "NestedClass::nested_method()" << std::endl;
        }
    };
};

int main() {
    MyClass::NestedClass nested_obj;
    nested_obj.nested_method(); // 调用嵌套类中的成员函数
    return 0;
}

In the code above, MyClass::NestedClassdouble colons are used to denote nested classes NestedClassin MyClassyes.

It should be noted that the specific grammatical rules of double colons representing namespace scope distinguishing symbols, scope qualifiers, and nested class or namespace qualifiers depend on the context and need to be understood and distinguished according to the context.

2. The meaning and function of single colon (:) in C++

In C++, the single colon ( :) can be used in a variety of ways, including:

        1. Indicates the inheritance relationship. In the definition of a class, use a single colon to declare the parent class of the current class, as follows:

class DerivedClass : public BaseClass {
    // ...
};

        2. Indicates member initialization. In the class constructor, use a single colon to initialize the members of the current object, as follows:

class MyClass {
public:
    MyClass(int n, double d) : num(n), dbl(d) {}
private:
    int num;
    double dbl;
};

 In the above code, the parameters of the constructor nand and two member variables dare initialized respectively .numdbl

        3. A scope resolution symbol representing a namespace. For std::coutexample, the double colons are used to indicate coutmembers stdof a namespace, not members of other namespaces.

        4. In conditional compilation, #definecommands can use single colons to define macro functions. For example:

#define MIN(x, y) ((x) < (y) ? (x) : (y))

It will MIN(x, y)be defined as a macro function that returns xthe yminimum of the sum.

It should be noted that the specific grammatical rules of the single colon indicating inheritance, member initialization and namespace scope resolution symbols depend on the context and need to be understood and distinguished according to the context.

Guess you like

Origin blog.csdn.net/qq_50635297/article/details/130028162