问题
I have a static library written in C++. I have also got the header files for the classes defined in the static library.
Can I access the private members of the classes defined in the static library introducing a friend function in class declaration ?
回答1:
You mean you want to change the headers that are shipped with the library? It's in no way guaranteed that adding friend
declarations there will work. You might mess up the linking part, even if your compiler says it's ok.
Also, if those members are private
, you just don't access them.
回答2:
It is technically undefined behavior to use a different sequence of tokens to define the same entity (here a class) in different Translation Units.
Whatever the technic you use, as long as it alters the sequence of tokens composing it, it is evil from the Standard point of view (though likely to work in practice).
Johannes discovered a way to do so while respecting the Standard. It is based on the fact that even though a
is a private attribute in class A
, &A::a
can be written in contexts that cannot write A.a
(perhaps an oversight in the Standard ?).
Core method:
template<typename Tag, typename Tag::type M>
struct Rob {
friend typename Tag::type get(Tag) {
return M;
}
};
// use
struct A {
A(int a):a(a) { }
private:
int a;
};
// tag used to access A::a
struct A_f {
typedef int A::*type;
friend type get(A_f);
};
template struct Rob<A_f, &A::a>;
int main() {
A a(42);
std::cout << "proof: " << a.*get(A_f()) << std::endl;
}
Extension for simplicity:
template<typename Tag, typename Member>
struct TagBase {
typedef Member type;
friend type get(Tag);
};
struct A_f : TagBase<A_f, int A::*> { };
EDIT:
This trick is (amusingly) explicitly allowed by the Standard
§14.7.2/12 The usual access checking rules do not apply to names used to specify explicit instantiations. [...]
来源:https://stackoverflow.com/questions/9903718/friend-function-access-the-private-members-of-class-defined-in-static-library