#include <iostream>
using namespace std;
class WhoAmI
{
public:
//맴버변수
int id;
//생성자
WhoAmI(int id_arg);
//맴버함수
void ShowYourself() const;
};
//생성자
WhoAmI::WhoAmI(int id_arg)
{
id = id_arg;
}
void WhoAmI::ShowYourself() const
{
cout << "{ ID = " << id << ", this = " << this << " }\n";
}
int main()
{
//세개의 객체를 만든다.
WhoAmI obj1(1);
WhoAmI obj2(2);
WhoAmI obj3(3);
//객체의 정보를 출력한다.
obj1.ShowYourself();
obj2.ShowYourself();
obj3.ShowYourself();
//객체의 주소를 출력한다.
cout << "&obj1 = " << &obj1 << "\n";
cout << "&obj2 = " << &obj2 << endl;
cout << "&obj3 = " << &obj3 << endl;
return 0;
}
예제 코드를 사용해서 각각 객체에서 this의 사용을 확인해보았다.
출력 값을 확인 해보면 this는 하나의 포인터이고, 각 객체 자체의 주소를 가지고 있는것으로 확인된다.