[编程开发] c++入门学习篇(1)之::作用域符解析

[复制链接]
ldwr 发表于 2023-10-3 19:23:09|来自:北京 | 显示全部楼层 |阅读模式
在C ++中,作用域运算符为::。它用于以下目的。
1)当存在具有相同名称的局部变量时,要访问全局变量:
  1. // C++ program to show that we can access a global variable
  2. // using scope resolution operator :: when there is a local  
  3. // variable with same name  
  4. #include<iostream>  
  5. using namespace std;
  6. int x;  // Global x
  7. int main()
  8. {
  9.   int x = 10; // Local x
  10.   cout << &#34;Value of global x is &#34; << ::x;
  11.   cout << &#34;\nValue of local x is &#34; << x;   
  12.   return 0;
  13. }
复制代码
输出:
全局x的值为0
本地x的值为10
2)在类之外定义函数。
  1. // C++ program to show that scope resolution operator :: is used
  2. // to DeFine a function outside a class
  3. #include<iostream>  
  4. using namespace std;
  5. class A  
  6. {
  7. public:  
  8.    // Only declaration
  9.    void fun();
  10. };
  11. // Definition outside class using ::
  12. void A::fun()
  13. {
  14.    cout << &#34;fun() called&#34;;
  15. }
  16. int main()
  17. {
  18.    A a;
  19.    a.fun();
  20.    return 0;
  21. }
复制代码
输出:
fun() called
3)访问一个类的静态变量。
  1. // C++ program to show that :: can be used to access static
  2. // members when there is a local variable with same name
  3. #include<iostream>
  4. using namespace std;
  5. class Test
  6. {
  7.     static int x;   
  8. public:
  9.     static int y;   
  10.     // Local parameter 'a' hides class member
  11.     // 'a', but we can access it using ::
  12.     void func(int x)   
  13.     {  
  14.        // We can access class's static variable
  15.        // even if there is a local variable
  16.        cout << &#34;Value of static x is &#34; << Test::x;
  17.        cout << &#34;\nValue of local x is &#34; << x;   
  18.     }
  19. };
  20. // In C++, static members must be explicitly defined  
  21. // like this
  22. int Test::x = 1;
  23. int Test::y = 2;
  24. int main()
  25. {
  26.     Test obj;
  27.     int x = 3 ;
  28.     obj.func(x);
  29.     cout << &#34;\nTest::y = &#34; << Test::y;
  30.     return 0;
  31. }
复制代码
输出:
静态x的值为1
本地x的值为3
测试:: y = 2
4)如果有多个继承:
如果两个祖先类中存在相同的变量名,则可以使用作用域运算符进行区分。
  1. // Use of scope resolution operator in multiple inheritance.
  2. #include<iostream>
  3. using namespace std;
  4. class A
  5. {
  6. protected:
  7.     int x;
  8. public:
  9.     A() { x = 10; }
  10. };
  11. class B
  12. {
  13. protected:
  14.     int x;
  15. public:
  16.     B() { x = 20; }
  17. };
  18. class C: public A, public B
  19. {
  20. public:
  21.    void fun()
  22.    {
  23.       cout << &#34;A's x is &#34; << A::x;
  24.       cout << &#34;\nB's x is &#34; << B::x;
  25.    }
  26. };
  27. int main()
  28. {
  29.     C c;
  30.     c.fun();
  31.     return 0;
  32. }
复制代码
输出:
A的x是10
B的x是20
5)对于命名空间
如果两个命名空间中都存在一个具有相同名称的类,则可以将名称空间名称与作用域解析运算符一起使用,以引用该类而不会发生任何冲突
  1. // Use of scope resolution operator for namespace.
  2. #include<iostream>
  3. int main(){
  4.     std::cout << &#34;Hello&#34; << std::endl;
  5. }
复制代码
在这里,cout和endl属于std命名空间。
6)在另一个类中引用一个类:
如果另一个类中存在一个类,我们可以使用嵌套类使用作用域运算符来引用嵌套的类
  1. // Use of scope resolution class inside another class.
  2. #include<iostream>
  3. using namespace std;
  4. class outside
  5. {
  6. public:
  7.       int x;
  8.       class inside
  9.       {
  10.       public:
  11.             int x;
  12.             static int y;  
  13.             int foo();
  14.       };
  15. };
  16. int outside::inside::y = 5;  
  17. int main(){
  18.     outside A;
  19.     outside::inside B;
  20. }
复制代码
全部回复3 显示全部楼层
海默子 发表于 2023-10-3 19:23:49|来自:北京 | 显示全部楼层
笔芯
jakcy 发表于 2023-10-3 19:24:34|来自:北京 | 显示全部楼层
你是优秀的
sexbobo 发表于 2023-10-3 19:24:53|来自:北京 | 显示全部楼层
[爱]

快速回帖

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则