logo

Operator rozpoznawania zakresu w C++

W C++ operatorem rozpoznawania zakresu jest :: . Jest używany do następujących celów.

1) Aby uzyskać dostęp do zmiennej globalnej, gdy istnieje zmienna lokalna o tej samej nazwie:



CPP
// 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  using namespace std; int x; // Global x int main() { int x = 10; // Local x cout << 'Value of global x is ' << ::x; cout << '
Value of local x is ' << x;  return 0; }>

Wyjście
Value of global x is 0 Value of local x is 10>

2) Aby zdefiniować funkcję poza klasą.

CPP
// C++ program to show that scope resolution operator :: is // used to define a function outside a class #include  using namespace std; class A { public:  // Only declaration  void fun(); }; // Definition outside class using :: void A::fun() { cout << 'fun() called'; } int main() {  A a;  a.fun();  return 0; }>

Wyjście
fun() called>

3) Aby uzyskać dostęp do zmiennych statycznych klasy.

CPP
// C++ program to show that :: can be used to access static // members when there is a local variable with same name #include using namespace std; class Test {  static int x;  public:  static int y;   // Local parameter 'x' hides class member  // 'x', 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 << 'Value of static x is ' << Test::x;  cout << '
Value of local x is ' << 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 << '
Test::y = ' << Test::y;  return 0; }>

Wyjście
Value of static x is 1 Value of local x is 3 Test::y = 2>

4) W przypadku dziedziczenia wielokrotnego: Jeśli ta sama nazwa zmiennej istnieje w dwóch klasach przodków, możemy użyć operatora rozpoznawania zakresu, aby to rozróżnić.



CPP
// Use of scope resolution operator in multiple inheritance. #include 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 << 'A's x is ' << A::x;  cout << '
B's x is ' << B::x; } }; int main() {  C c;  c.fun();  return 0; }>

Wyjście
A's x is 10 B's x is 20>

5) Dla przestrzeni nazw Jeśli klasa o tej samej nazwie istnieje w dwóch przestrzeniach nazw, możemy użyć nazwy przestrzeni nazw z operatorem rozpoznawania zakresu, aby odwołać się do tej klasy bez żadnych konfliktów

C++
#include  #include  using namespace std; #define nline '
' // Global Declarations string name1 = 'GFG'; string favlang = 'python'; string companyName = 'GFG_2.0'; // You can also do the same thing in classes as we did in // our struct example class Developer { public:  string name = 'krishna';  string favLang = 'c++';  string company = 'GFG';  // Accessing Global Declarations  Developer(string favlang, string company)  : favLang(favlang)  , company(companyName)  {  } }; int main() {  Developer obj = Developer('python', 'GFG');  cout << 'favourite Language : ' << obj.favLang << endl;  cout << 'company Name : ' << obj.company << nline; }>

Wyjście
favourite Language : python company Name : GFG_2.0>

6) Odwołaj się do klasy wewnątrz innej klasy: Jeśli klasa istnieje wewnątrz innej klasy, możemy użyć klasy zagnieżdżającej, aby odwołać się do klasy zagnieżdżonej za pomocą operatora rozpoznawania zakresu

CPP
// Use of scope resolution class inside another class. #include  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; }>

7) Odwołaj się do elementu klasy bazowej w obiekcie pochodnym: W przypadku posiadania tej samej metody zarówno w klasie podstawowej, jak i pochodnej, moglibyśmy odnosić się do każdej z nich za pomocą operatora rozpoznawania zakresu, jak poniżej.



C++
// Refer to a member of the base class in the derived object. #include  class Base { public:  void func()  {  std::cout << 'This is Base class' << std::endl;  } }; class Derived : public Base { public:  void func()  {  std::cout << 'This is Derived class' << std::endl;  } }; int main() {  Derived obj;  obj.Base::func();  obj.func();  return 0; }>

Wyjście
This is Base class This is Derived class>