글
20141010 (C++ this)
155일차
-------------
C++
-------------
------- this
현재 객체를 가리킨다
(객체 주소)
예제)
#include <iostream>
#include <cstring>
using namespace std;
class SoSimple
{
public:
int num;
public:
SoSimple(int n)
: num(n)
{
cout << "num=" << num << ", ";
cout << "address=" << this << endl;
}
void ShowSimpleData()
{
cout << num << endl;
}
SoSimple * GetThisPointer()
{
return this;
}
};
int main()
{
SoSimple sim1(100);
SoSimple * ptr1 = sim1.GetThisPointer();
cout << ptr1 << ", ";
ptr1->ShowSimpleData();
SoSimple sim2(200);
SoSimple * ptr2 = sim2.GetThisPointer();
cout << ptr2 << ", ";
ptr2->ShowSimpleData();
return 0;
}
결과
다른 예제)
#include <iostream>
using namespace std;
class SelfRef
{
private:
int num;
public:
SelfRef(int n)
: num(n)
{
cout << "객체생성" << endl;
}
SelfRef & Adder(int n)
{
num = num + n;
return *this;
}
SelfRef & ShowTwoNumber()
{
cout << num << endl;
return *this;
}
};
int main()
{
SelfRef obj(3);
SelfRef & ref = obj.Adder(2);
obj.ShowTwoNumber();
ref.ShowTwoNumber();
ref.Adder(1).ShowTwoNumber().Adder(2).ShowTwoNumber();
return 0;
}
결과
--------------
RFID
--------------
'부산IT학원 > 스마트컨트롤러' 카테고리의 다른 글
20141014 (C++ class내에 const, friend, static, 시리얼 통신 winapi) (0) | 2014.10.14 |
---|---|
20141013 (C++ 복사 생성자, 시리얼 통신 winapi) (0) | 2014.10.13 |
20141008 (C++ 맴버 이니셜라이저) (0) | 2014.10.08 |
20141007 (C++ 정보은닉, 캡슐화, 생성자, 소멸자, 비디오처리) (0) | 2014.10.07 |
20141006 (C++ struct, class) (0) | 2014.10.06 |