C++实现C#的get,set属性操作
2010-10-20 16:24
2336 查看
原文地址: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; }
相关文章推荐
- C++中的property库的设计与实现过程(二)——如何为属性指定get_和set_函数?
- C# 无参属性 set与get
- c++ 非托管程序调用c#托管程序l的实现操作方式
- c#中快速定义属性get/set 方法
- C# 中get和set属性的作用
- C#属性简写用法 {get;set}
- 深入了解隐式属性和带实现值属性的区别(属性中get{},set{}访问器小解)
- C#定义属性及其关键字get,set,value
- [Play with T]_[C# 类与属性操作] 通过克隆实现ObjectMap对List的填充
- 在C++实现C#中的属性(Property)功能的尝试: MACRO篇(1)
- category使用 objc_setAssociatedObject/objc_getAssociatedObject 实现添加属性
- C#中get和set两个属性的辨别
- C#中的get和set有什么用 声明属性
- C#中get与set(类的属性封装)
- C#.NET中的定义类成员(属性)(关键字get,set)
- C++中实现C#属性相似的语法
- PHP使用方法重载实现动态创建属性的get和set方法
- 在C++实现C#中的属性(Property)功能的尝试: MACRO篇(2)
- c#属性中get和set的作用 c#多窗口 lable控件背景色透明