2019/12/13 - [졸작 진행과정 기록/GameEngine] - 20191213
다음 사이트에서 찾았다.
https://www.codeproject.com/Articles/118921/C-Properties
C++ Properties
C#-Style Properties in C++
www.codeproject.com
#include <iostream>
using namespace std;
#define PROPERTY(t,n) __declspec( property (put = property__set_##n, get = property__get_##n)) t n; \
typedef t property__tmp_type_##n
#define READONLY_PROPERTY(t,n) __declspec( property (get = property__get_##n) ) t n;\
typedef t property__tmp_type_##n
#define WRITEONLY_PROPERTY(t,n) __declspec( property (put = property__set_##n) ) t n;\
typedef t property__tmp_type_##n
#define GET(n) property__tmp_type_##n property__get_##n()
#define SET(n) void property__set_##n(const property__tmp_type_##n& value)
class Color
{
private:
float color[4];
public:
Color();
Color(float r, float g, float b);
Color(float r, float g, float b, float a);
PROPERTY(float, r);
GET(r) { return color[0]; }
SET(r) { color[0] = r; }
PROPERTY(float, g);
GET(g) { return color[1]; }
SET(g) { color[1] = g; }
PROPERTY(float, b);
GET(b) { return color[2]; }
SET(b) { color[2] = b; }
PROPERTY(float, a);
GET(a) { return color[3]; }
SET(a) { color[3] = a; }
};
int main()
{
Color c;
c.r = .5f;
float red = c.r;
cout << sizeof(Color) << endl; //16
}
Microsoft 확장 문법을 사용한 __declspec과 property 연산자를 활용하기 때문에 Visual Studio가 아니면 컴파일이 안될 가능성이 높다.
어쨌든 이런 식으로 만들어도 r,g,b,a라는 변수가 따로 만들어지지 않고 크기는 16으로 고정되어있는 것을 보면 완벽한 C# 스타일 프로퍼티로 사용 가능할 듯 하다.
GameEngine 일기에만 써놓는 것 보다 따로 이렇게 글을 써두면 좋을 것 같아서 써둔다.
'Programming' 카테고리의 다른 글
| [C++] noexcept 선언 (1) | 2019.12.19 |
|---|---|
| [C++] Exception 처리 (0) | 2019.12.19 |
| 멀티쓰레딩 연습 (0) | 2019.12.18 |
| Enum class 에 대한 한가지 사실 (0) | 2019.12.16 |
| 스마트 포인터 (1) | 2019.12.13 |