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?