ldwr 发表于 2023-10-3 19:23:09

c++入门学习篇(1)之::作用域符解析

在C ++中,作用域运算符为::。它用于以下目的。
1)当存在具有相同名称的局部变量时,要访问全局变量:
// C++ program to show that we can access a global variable
// using scope resolution operator :: when there is a local
// variable with same name
#include<iostream>
using namespace std;

int x;// Global x

int main()
{
int x = 10; // Local x
cout << &#34;Value of global x is &#34; << ::x;
cout << &#34;\nValue of local x is &#34; << x;   
return 0;
} 输出:
全局x的值为0
本地x的值为102)在类之外定义函数。
// C++ program to show that scope resolution operator :: is used
// to define a function outside a class
#include<iostream>
using namespace std;

class A
{
public:

   // Only declaration
   void fun();
};

// Definition outside class using ::
void A::fun()
{
   cout << &#34;fun() called&#34;;
}

int main()
{
   A a;
   a.fun();
   return 0;
} 输出:
fun() called3)访问一个类的静态变量。
// C++ program to show that :: can be used to access static
// members when there is a local variable with same name
#include<iostream>
using namespace std;

class Test
{
    static int x;   
public:
    static int y;   

    // Local parameter 'a' hides class member
    // 'a', but we can access it using ::
    void func(int x)   
    {
       // We can access class's static variable
       // even if there is a local variable
       cout << &#34;Value of static x is &#34; << Test::x;

       cout << &#34;\nValue of local x is &#34; << x;   
    }
};

// In C++, static members must be explicitly defined
// like this
int Test::x = 1;
int Test::y = 2;

int main()
{
    Test obj;
    int x = 3 ;
    obj.func(x);

    cout << &#34;\nTest::y = &#34; << Test::y;

    return 0;
} 输出:
静态x的值为1
本地x的值为3
测试:: y = 24)如果有多个继承:
如果两个祖先类中存在相同的变量名,则可以使用作用域运算符进行区分。
// Use of scope resolution operator in multiple inheritance.
#include<iostream>
using namespace std;

class A
{
protected:
    int x;
public:
    A() { x = 10; }
};

class B
{
protected:
    int x;
public:
    B() { x = 20; }
};

class C: public A, public B
{
public:
   void fun()
   {
      cout << &#34;A's x is &#34; << A::x;
      cout << &#34;\nB's x is &#34; << B::x;
   }
};

int main()
{
    C c;
    c.fun();
    return 0;
}输出:
A的x是10
B的x是205)对于命名空间
如果两个命名空间中都存在一个具有相同名称的类,则可以将名称空间名称与作用域解析运算符一起使用,以引用该类而不会发生任何冲突
// Use of scope resolution operator for namespace.
#include<iostream>
int main(){
    std::cout << &#34;Hello&#34; << std::endl;
} 在这里,cout和endl属于std命名空间。
6)在另一个类中引用一个类:
如果另一个类中存在一个类,我们可以使用嵌套类使用作用域运算符来引用嵌套的类
// Use of scope resolution class inside another class.
#include<iostream>
using namespace std;

class outside
{
public:
      int x;
      class inside
      {
      public:
            int x;
            static int y;
            int foo();

      };
};
int outside::inside::y = 5;

int main(){
    outside A;
    outside::inside B;

}

海默子 发表于 2023-10-3 19:23:49

笔芯

jakcy 发表于 2023-10-3 19:24:34

你是优秀的

sexbobo 发表于 2023-10-3 19:24:53

[爱]
页: [1]
查看完整版本: c++入门学习篇(1)之::作用域符解析