参阅了1些文章,做了1个vc、cb、gcc通用的property声明宏函数,经cb和devcpp测试通过,release版本的效力没有损失。真不明白c++委员会是干吗吃的,整天弄那个标准库,却不肯扩充基本功能。面向对象的类的3个操作:方法、属性、事件,其中属性就不肯提供编辑器支持标准,cb和vc多少年前就支持了,不是c++做不到,只是编程进程中,那些get()和set()之类的操作函数既不美观,书写也费力,整体看起来,括号()太多了!
代码以下:
//property
#if defined(__BORLANDC__) || defined(_MSC_VER)#define property_get_set(class,type,name,getter,setter)
__declspec(property(get=getter,put=setter)) type name;
#define property_get(class,getter)
__declspec(property(get=getter)) type name;
#define property_begin
#define property_end
#else //defined(__BORLANDC__) || defined(_MSC_VER)
//摹拟的property是占用空间的
#define property_get_set(class_t,type_t,SimSun; font-size:14px; line-height:24px">class property_##name
{
public:
friend class class_t;
inline size_t offset(){return offsetof(class_t,name); }
inline class_t* parent(){ return reinterpret_cast<class_t*>(size_t(this)-offset()); }
template<typename T> inline operator T()
return T(parent()->getter());
}
inline property_##name & operator=(const type_t& n)
parent()->setter(n);
return *this;
}name;
#define property_get(class_t,SimSun; font-size:14px; line-height:24px">inline size_t offset(){ return offsetof(class_t,SimSun; font-size:14px; line-height:24px">template<typename T>inline operator T()
private:
property_##name & operator=(const type_t& n);
//把property放到1个union里面,可以有效减少类空间的占用
#define property_begin union{
#define property_end };
#endif//defined(__BORLANDC__) || defined(_MSC_VER)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。