I'd like to create an object used to store data, restricting read/write access.
For example :
OBJ obj1;
OBJ obj2;
// DataOBJ has 2 methods : read() and write()
DataOBJ dataOBJ1 (obj1);
With the code above, I want obj1
to access write()
method, while other OBJ
objects (obj2
in this case) should only access the read()
method.
Is it possible to create a DataOBJ
class restricting rights like that ?
The classical "getter setter" does not suit my needs.
Thanks.
You can control access to write/read by template global reference obj1/obj2 like in this example:
class OBJ {
};
OBJ obj1;
OBJ obj2;
// RESTRICTED ACCESS
class DataOBJBase {
protected:
void write() {}
void read() {}
};
template <OBJ&>
class DataOBJ;
// ALLOW WRITE IF FOR obj1
template <>
class DataOBJ<obj1> : public DataOBJBase {
public:
using DataOBJBase::write;
};
// ALLOW READ IF FOR obj2
template <>
class DataOBJ<obj2> : public DataOBJBase {
public:
using DataOBJBase::read;
};
int main() {
DataOBJ<obj1> dobj1;
dobj1.write(); // cannot read
DataOBJ<obj2> dobj2;
dobj2.read(); // cannot write
}