I'm writing a reflection utility for self usage, I simplified the code(remove complicated templates) as below:
class A {
private:
friend class Field;
int i;
};
class Field {
public:
Field(int A::* p) : p(p) {}
private:
int A::* p;
};
Field field(&A::i);
the compiler complains about i is private of class A.
I'm confused now, I know that parameters have a scope of function prototype scope, but I don't know much about it's relationship with the C++ feature friend classes.
Can anyone help what should I do to pass the private &A::i to the field object?
In the line
you are accessing a private data member pointer (
&A::i) from within a toplevel scope. This is not related to the classFieldbeing afriendofA. What you could do instead, is givingfriendaccess to a function that is supposed to instantiateFieldobjects, e.g.The definition of
Fielditself can remain unchanged, while the builder function could be