当前位置:   article > 正文

C++基础-作用域运算符_c++作用域运算符

c++作用域运算符

在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 << "Value of global x is " << ::x;
  11. cout << "\nValue of local x is " << x;
  12. return 0;
  13. }

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 << "fun() called";
  15. }
  16. int main()
  17. {
  18. A a;
  19. a.fun();
  20. return 0;
  21. }

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 << "Value of static x is " << Test::x;
  17. cout << "\nValue of local x is " << 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 << "\nTest::y = " << Test::y;
  30. return 0;
  31. }

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 << "A's x is " << A::x;
  24. cout << "\nB's x is " << B::x;
  25. }
  26. };
  27. int main()
  28. {
  29. C c;
  30. c.fun();
  31. return 0;
  32. }

5.对于命名空间

如果两个命名空间中都存在一个具有相同名称的类,则可以将名称空间名称与作用域解析运算符一起使用,以引用该类而不会发生任何冲突

  1. // Use of scope resolution operator for namespace.
  2. #include<iostream>
  3. int main(){
  4. std::cout << "Hello" << 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. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/51597
推荐阅读
相关标签
  

闽ICP备14008679号