I have the following design problem:
I have a Resource with two sorts of accessors:
- one is to modify it (let’s call it
Access) - one is for const-like access (let’s call it
Const_access), but you could say c1=c2 and then c1 will access c2.
Given that Resource is big, I have to achieve the following copy mechanism:
Access->Access: deep copy
Access->Const_access: deep copy
Const_access->Access: deep copy
Const_access->Const_access: shallow copy
I aim to write Access so that Const_access will be able to use exactly the const functions in Access.
My current implementation is flawed, using:
class Access {
public:
Access(const Access&); // deep copy
void method(const Access&);
void const_method() const;
protected:
Resource res;
};
class Const_access : public Access{
private:
void method(); // only declaration
public:
Const_access(const Const_accesss&); // shallow copy
explicit Const_access(const Access&); // deep copy
};
but here Const_access ca; ca.Access::method() still works and I have to manually hide away the non-const accessors. I have tried protected or private inheritance but that prohibits flexibility for Access& to handle Const_Access& too.
What would be the correct solution for this problem?
This problem can simply solved using so called lazy evaluation:
making the private clone of the class resources only when a member function wants to modify it. R/W and Read-only access to resources is easily solved with private inheritance.
This way also the LSP is abided:
Objis now flawlessly publicly inherited fromConst_obj, if necessary at all.There is a link for the complete answer.