Возможные реализации для решения конкретных задач
Property
Свойство. Специализация для ReadOnly и WriteOnly.
#include <iostream>
using namespace std;struct ReadOnly_tag {};
struct WriteOnly_tag {};
struct ReadWrite_tag {};template <typename Owner, typename Type, typename Access = ReadWrite_tag>
class Property
{
using Getter = Type(Owner::*)() const;
using Setter = void (Owner::*)(const Type&);
private:
Owner* owner;
Getter methodGet;
Setter methodSet;
public:
Property() = default;
Property(Owner* const owr, Getter getmethod, Setter setmethod) : owner(owr), methodGet(getmethod), methodSet(setmethod) {}
void init(Owner* const owr, Getter getmethod, Setter setmethod)
{
owner = owr;
methodGet = getmethod;
methodSet = setmethod;
}
operator Type() // Getter
{
return (owner->*methodGet)();
}
void operator=(const Type& data) // Setter
{
(owner->*methodSet)(data);
}
};void main()
{
Object obj(5., 15., 25.);
obj.ValueRW = 10.;
cout << "value = " << obj.ValueRW << endl;
// obj.ValueRO = 10.; // Error! (ReadOnly)
cout << "value = " << obj.ValueRO << endl;
obj.ValueWO = 10.;
// cout << "value = " << obj.ValueWO << endl; // Error! (WriteOnly)
}Last updated