범위 확인 연산자는 정적 또는 클래스 멤버에 액세스하는 데 사용되는 반면 이 포인터는 동일한 이름의 로컬 변수가 있는 경우 개체 멤버에 액세스하는 데 사용됩니다.
범위 확인 연산자
예시
#include<iostream>
using namespace std;
class AB {
static int x;
public:
// Local parameter 'x' hides class member
// 'x', but we can access it using ::.
void print(int x) {
cout<<"the number is:" << AB::x;
}
};
// static members must be explicitly defined like below in c ++
int AB::x = 7;
int main() {
AB ob;
int m = 6 ;
ob.print(m);
return 0;
} 출력
the number is:7
이 포인터
예시
#include<iostream>
using namespace std;
class AB {
int x;
public:
AB() {
x = 6;
}
// here Local parameter 'x' hides object's member
// 'x', we can access it using this.
void print(int x) {
cout<<"the number is: " << this->x;
}
};
int main() {
AB ob;
int m = 7 ;
ob.print(m);
return 0;
} 출력
the number is: 6