|
- template <typename T>
- class _property{
- public:
- using Get = std::function<T()>;
- using Set = std::function<void(const T&)>;
- _property(Get fng, Set fns){ _get = fng; _set = fns; }
- const T& operator=(const T& value){ _set(value); return value; }
- operator T(){ return _get(); }
- private:Get _get; Set _set;
- };
- #define PROPERTY(Type,get,set,name)\
- _property<Type> name = { _property<Type>::Get([&]()##get), _property<Type>::Set([&](const Type& value)##set) }
- class sample{
- public:
- PROPERTY(int,
- {
- std::cout << "property get" << std::endl;
- return ival;
- },
- {
- ival = value;
- std::cout << "property set" << std::endl;
- },
- INTVAL);
- private:
- int ival;
- };
- int main()
- {
- sample sp;
- sp.INTVAL = 1;
- std::cout << (sp.INTVAL + 5) << std::endl;
- std::cin.get();
- }
复制代码
|
|