Sel*_*rio 1 c++ inheritance nested-class
这是一个奇怪的问题,但我使用嵌套类在c ++中创建类似属性的功能.我还通过使嵌套类的赋值运算符受到保护以及嵌套类的主机类a使这些工作成为ReadOnly Friend.不幸的是,似乎任何继承类(with : public BaseClass)仍然无法访问嵌套类的赋值运算符.
我不知道它是否重要,但我确实把它放在Friend了课堂的公共部分.
有谁知道如何解决这个问题?(或者破解它)谢谢
编辑:添加了代码示例
#define ReadOnlyProperty(type,name,parent) \
protected: \
class name : public PropertyBase<type> \
{ \
parent* This; \
public: \
friend class parent; \
name(parent* instance) { This = instance; } \
protected: \
void operator=(type value) {Set(value);} \
void Set(type value); \
type Get() const; \
}; \
public: name name; \
friend class name; \
private:
Run Code Online (Sandbox Code Playgroud)
基类:
class Object
{
ReadOnlyProperty(char,Type,Object);
public:
Object() : Type(this) {}
};
Run Code Online (Sandbox Code Playgroud)
继承类
class A : public Object
{
public:
A() {Type = 'A';}
};
Run Code Online (Sandbox Code Playgroud)
我在Type ='A'上收到错误
友善不是遗传的.
目前尚不清楚最佳解决方案是什么; 或许在基类的受保护部分声明嵌套类,并使其成员公开?这样就不需要任何友谊,但是来自类层次结构之外的东西将无法摆弄它.