再谈用c++实现property,不明白那些委员会是干嘛吃的

参阅了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)

相关文章

文章浏览阅读8.4k次,点赞8次,收藏7次。SourceCodester Onl...
文章浏览阅读3.4k次,点赞46次,收藏51次。本文为大家介绍在...
文章浏览阅读1.1k次。- php是最优秀, 最原生的模板语言, 替代...
文章浏览阅读1.1k次,点赞18次,收藏15次。整理K8s网络相关笔...
文章浏览阅读1.2k次,点赞22次,收藏19次。此网络模型提供了...
文章浏览阅读1.1k次,点赞14次,收藏19次。当我们谈论网络安...