Home > Backend Development > C#.Net Tutorial > What is :: in c language

What is :: in c language

下次还敢
Release: 2024-04-13 18:42:29
Original
663 people have browsed it

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.

What is :: in c language

Double colon (::) in C

In C, double colon (::) operation The symbol has the following uses:

1. Global namespace access

  • Used when accessing global variables or functions not declared in the current namespace.
  • For example:

    <code class="cpp">::x = 10; // 访问全局变量 x</code>
    Copy after login

2. Namespace qualification

  • Specify the variable or function to which Namespaces.
  • For example:

    <code class="cpp">namespace std {
    int a;
    }
    
    int main() {
    std::a = 10; // 访问 std 命名空间中的变量 a
    }</code>
    Copy after login

3. Enumeration constant access

  • Access in enumeration type used when using constants.
  • For example:

    <code class="cpp">enum Color {
    Red,
    Green,
    Blue
    };
    
    int main() {
    Color color = ::Color::Red; // 访问枚举常量 Red
    }</code>
    Copy after login

4. Static method call

  • Used when calling static class methods .
  • For example:

    <code class="cpp">class MyClass {
    public:
      static void print() {
        cout << "Hello!" << endl;
      }
    };
    
    int main() {
    MyClass::print(); // 调用静态方法 print
    }</code>
    Copy after login

5. Base class reference

  • Reference the base in the derived class Used when class.
  • 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>
    Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template