我想在不修改 .h 文件的情况下访问私有成员。这是 .h 文件的示例
class X
{
private:
class A
{...};
vector<A> arr;
}
Run Code Online (Sandbox Code Playgroud)
问:如何访问 X::arr ?
class X
{
private:
int a;
};
template<typename Tag, typename Tag::type Member>
struct XAccessor
{
friend typename Tag::type get(Tag)
{
return Member;
}
};
struct X_A
{
typedef int X::* type;
friend type get(X_A);
};;
template struct XAccessor<X_A, &X::a>;
...
auto x = new X;
x->*get(X_A())=11;
...
Run Code Online (Sandbox Code Playgroud)
我在网上找到了这种方法,但是当我更改typedef int X::* type为时typedef vector<X::A> X::* type,它给了我一个错误,说X::A无法访问。