1

this is rather a strange question but I am using nested classes to make property-like functionality in c++. I also made these work as ReadOnly by making the nested class's assignment operator protected and the host class a Friend of the nested class. Unfortunately it seems that any inheriting class (with : public BaseClass) still cant access the nested class's assignment operator.

I dont know if it matters but I did put the Friend in the public section of the class.

Anyone know how I can fix this? (or hack it) Thanks

EDIT: code example added

#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:

Base class:

class Object
{
ReadOnlyProperty(char,Type,Object);

public:

Object() : Type(this) {}
};

Inheriting class

class A : public Object
{
public:
A() {Type = 'A';}
};

i get an error on the Type = 'A'

4

2 回答 2

6

Friendliness is not inherited.

It's not clear what the best solution is; perhaps declare the nested class in the protected section of the base class, and make its members public? That way no friendship is needed, but stuff from outside the class hierarchy won't be able to fiddle with it.

于 2013-01-07T01:34:25.013 回答
0

你把朋友声明放在哪个部分都没有关系。

也许,相反,将内部类放在受保护的部分,并将其接口公开。

于 2013-01-07T01:42:54.073 回答