Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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'

share|improve this question
    
You can't automatically make all inherited classes friends. If you put a piece of code, we can see what a good workaround might look like. – leemes Jan 7 '13 at 1:45

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.

share|improve this answer
1  
+1 for extreme succinctness – Ben Jan 7 '13 at 1:37
    
I would never usually upvote such a short and non-detailed answer, but there are many reasons that this one deserves it for this question. I can't be bothered to list them. And I know you know them anyway. – Lightness Races in Orbit Jan 7 '13 at 1:37
2  
+1 I actually love answers like this. The only reason I don't post them very often is that for some reason people like reading more words than they need to, and my answer ends up getting downvoted as a result. – Mehrdad Jan 7 '13 at 1:38
    
Well, it isn't an answer to the question (How to fix this?). But answering this question requires a redesign, I think, so there is no straight-forward solution. Is there? – leemes Jan 7 '13 at 1:39
    
Code eg uploaded, i hope this makes it all more clear. – Mr Universe Jan 7 '13 at 3:03

and it does not matter which section you put the friend declaration.

perhaps, instead, put the inner class in the protected section, and make its interfaces public.

share|improve this answer
1  
Please write in complete sentences. – leemes Jan 7 '13 at 1:44
    
Putting the class in protected has no effect. – Mr Universe Jan 7 '13 at 2:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.