小编yaw*_*w x的帖子

如何在 C++ 中访问私有类型的私有成员?

我想在不修改 .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无法访问。

c++ templates boost c++-loki

-1
推荐指数
1
解决办法
171
查看次数

标签 统计

boost ×1

c++ ×1

c++-loki ×1

templates ×1