logo

C++의 범위 확인 연산자

C++에서 범위 확인 연산자는 다음과 같습니다. :: . 다음과 같은 목적으로 사용됩니다.

1) 동일한 이름을 가진 지역 변수가 있을 때 전역 변수에 액세스하려면 다음을 수행하십시오.



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; }>

산출
Value of global x is 0 Value of local x is 10>

2) 클래스 외부에서 함수를 정의합니다.

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; }>

산출
fun() called>

3) 클래스의 정적 변수에 액세스합니다.

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; }>

산출
Value of static x is 1 Value of local x is 3 Test::y = 2>

4) 다중 상속의 경우: 두 상위 클래스에 동일한 변수 이름이 존재하는 경우 범위 확인 연산자를 사용하여 구별할 수 있습니다.



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; }>

산출
A's x is 10 B's x is 20>

5) 네임스페이스의 경우 동일한 이름을 가진 클래스가 두 네임스페이스 내에 존재하는 경우 범위 확인 연산자와 함께 네임스페이스 이름을 사용하여 충돌 없이 해당 클래스를 참조할 수 있습니다.

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; }>

산출
favourite Language : python company Name : GFG_2.0>

6) 다른 클래스 내의 클래스를 참조하십시오. 클래스가 다른 클래스 내에 존재하는 경우 중첩 클래스를 사용하여 범위 확인 연산자를 사용하여 중첩 클래스를 참조할 수 있습니다.

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) 파생 객체에서 기본 클래스의 멤버를 참조합니다. 기본 클래스와 파생 클래스 모두 동일한 메서드를 갖는 경우 아래와 같이 범위 결정 연산자로 각각을 참조할 수 있습니다.



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; }>

산출
This is Base class This is Derived class>