C++实现C#的get,set属性操作

原文地址:http://www.codeproject.com/KB/cpp/properties.aspx
// 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;
}  
 

转载于:https://www.cnblogs.com/marryZhan/archive/2010/10/20/2213936.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值