码迷,mamicode.com
首页 > 编程语言 > 详细

C++ Properties set get

时间:2015-02-13 11:54:41      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

// properties.h


#ifndef _PROPERTIES_H

#define _PROPERTIES_H


#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)


#endif /* _PROPERTIES_H */ 




// main.cpp

#include <iostream>


#include <math.h>


#include "properties.h"


class Vector2

{

public:

float x;

float y;


READONLY_PROPERTY(float, Length);

GET(Length) 

return sqrt((x*x + y*y));

}

};


int main()

{

Vector2 vec;

vec.x = 1;

vec.y = 1;

std::cout << "Length of vector(" << vec.x << ", " << vec.y << ") = ";

std::cout << vec.Length << "\n"; // <--- property, not a function call


return 0;

}  

The above code works with any Visual C++ version above 6.0. It does not need any additional dependencies -- as the section title states, it is a full example.


C++ Properties set get

标签:

原文地址:http://my.oschina.net/u/2256217/blog/378296

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!