наследование — Защищено & quot; только для чтения & quot; прокси-класс для примитивов в Stack Overflow

Я недавно наткнулся этот прокси класс для того, чтобы сделать примитивные члены c ++ «только для чтения» (публично выступать в качестве константных ссылок, но в частном порядке не const). Это потенциально исключает необходимость использования «добытчиков». Прокси-класс выглядит так:

template <Container,Primitive>
class RO
{
friend Container;
public:
inline operator Primitive() const             { return x; }
template<typename Other> inline bool  operator==(const Other& y) const { return x == y; }
template<typename Other> inline bool  operator!=(const Other& y) const { return x != y; }
template<typename Other> inline bool  operator< (const Other& y) const { return x < y; }
template<typename Other> inline bool  operator> (const Other& y) const { return x > y; }
template<typename Other> inline bool  operator<=(const Other& y) const { return x <= y; }
template<typename Other> inline bool  operator>=(const Other& y) const { return x >= y; }
template<typename Other> inline Other operator+ (const Other& y) const { return x + y; }
template<typename Other> inline Other operator- (const Other& y) const { return x - y; }
... // all other const operator overloads
protected:
inline RO()                                  :x(){ }
template<typename Other> inline RO(const Other& y)                    :x(y) { }
template<typename Other> inline Primitive& operator= (const Other& y) { return x = y; }
template<typename Other> inline Primitive& operator+=(const Other& y) { return x += y; }
template<typename Other> inline Primitive& operator-=(const Other& y) { return x -= y; }
template<typename Other> inline Primitive& operator*=(const Other& y) { return x *= y; }
template<typename Other> inline Primitive& operator/=(const Other& y) { return x /= y; }
... // all other non-const operator overloads
inline Primitive* ptr()                      { return &x; }
inline Primitive& ref()                      { return x; }
inline const Primitive* const_ptr() const    { return &x; }
inline const Primitive& const_ref() const    { return x; }
Primitive x;
};

Это, кажется, работает как хотелось бы, если у меня есть плоская структура класса: только некоторые class A у которого есть RO прокси для его только для чтения члены. Но как только у меня появятся какие-то class B : public Aтогда я попаду в беду. Я хотел бы иметь B читать и писать в только для чтения RO члены, определенные в унаследованном классе A, Но видение дружбы не передается по наследству. Я не уверен, как это сделать. То же относится и к прямым друзьям A: дружба непереходна.

Существует ли конструкция «только для чтения» прокси, которые доступны для записи друзьям и производным классам?

1

Решение

Задача ещё не решена.

Другие решения