使用C++11实现类似C#的属性概念设计
来源:互联网 发布:程序员的发展趋势 编辑:程序博客网 时间:2024/06/10 09:29
原文地址在这:http://www.cnblogs.com/feng-sc/p/5742689.html
实现挺有意思的,看懂了代码也可以加深对C++知识的理解,是不错的小技巧,记录下来。
#include<functional>#define property_setter(variableType) [&](variableType value)#define property_getter(variableType) [&]()->variableTypetemplate <typename ValueType>class property_rw{public:typedef std::function<void(ValueType value)> Setter;typedef std::function<ValueType(void)> Getter;explicit property_rw(Setter setter, Getter getter) : m_setter(setter), m_getter(getter) {}property_rw& operator=(ValueType value){this->value = value;m_setter(this->value);return *this;}property_rw& operator=(const property_rw & instance){this->value = instance.m_getter();m_setter(this->value);return *this;}operator ValueType(){return m_getter();}private:ValueType value;Setter m_setter;Getter m_getter;};template <typename ValueType>class property_r{public:typedef std::function<void(ValueType value)> Setter;typedef std::function< ValueType (void)> Getter;explicit property_r(Getter getter) : m_getter(getter) {}operator ValueType(){return m_getter();}private:ValueType value;Getter m_getter;};template <typename ValueType>class property_w{public:typedef std::function <void(ValueType value)> Setter;explicit property_w(Setter setter) : m_setter(setter) {}property_w& operator=(ValueType value){this->value = value;m_setter(this->value);return *this;}property_w& operator=(const property_w & instance){this->value = instance.m_getter();m_setter(this->value);return *this;}private:ValueType value;Setter m_setter;};
#include <iostream>#include <string>#include "test.h"using namespace std;class TestDemo{public:property_rw<string> Name = property_rw<string>(property_setter(std::string){m_rw_name = value;},property_getter(std::string){return m_rw_name;});property_r<string> ReadOnlyName = property_r<string>(property_getter(std::string){return m_readonly_name;});property_w<string> WriteOnlyName = property_w<string>(property_setter(std::string){m_writeonly_name = value;TestWork();});void TestWork(){std::cout <<"TestWork()::m_writeonly_name--" << std::endl;}private:std::string m_rw_name;std::string m_readonly_name = "I m read only name";std::string m_writeonly_name = "";};int _tmain(int argc, _TCHAR* argv[]){TestDemo test;test.Name = "This is test name!";std::string str = test.Name;std::string readonly = test.ReadOnlyName;std::cout<< "Test read and write,Name:" <<str<< std::endl;std::cout<< "Test readonly, msg:"<< readonly << std::endl;test.WriteOnlyName = "This is write only property!";//test.ReadOnlyName = "This is write only property!";//std::string s = test.WriteOnlyName;return 0;}
0 0
- 使用C++11实现类似C#的属性概念设计
- 用C++实现类似C#属性
- 使用C实现类似Java ArrayList数据结构
- C#实现类似QQ的窗体停靠
- 在c#中实现类似c/c++函数指针的功能
- C实现的类似vector的容器
- C#自定义控件添加类似Size一样的结构属性
- 使用C#的属性
- C# 属性的使用
- C#: 类似BackGroundWorker功能的实现
- C++:实现类似MFC的IsKindOf功能
- 实现类似ping功能的C源代码
- C#Winform实现类似Android的Listener
- 使用javascript ajax C#实现类似百度的搜索框效果
- golang指针的使用,类似C语言
- C++ 实现类似java c#List的方法 boost::any的使用
- 使用Fragment实现类似Tab的需求
- 类似购物车小红点的实现-BadgeView使用
- android适配
- 归并排序
- tcp服务器与客户端
- list,set练习和Map简述
- wxWidgets界面设计工具DialogBlocks
- 使用C++11实现类似C#的属性概念设计
- Jackson之JSON包的使用分析
- 数据结构实验之图论二:基于邻接表的广度优先搜索遍历
- 数据结构实验之查找六:顺序查找
- Android Activity 四种启动模式
- 数据流中的中位数
- 不用正则实现js中replace全局替换的功能
- 九年来,移动互联测试有什么重要进展
- 理解CSS3 transform中的Matrix(矩阵)