#include

template
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;

int main(){
    A a(42);
    std::cout << "proof:" << a.*get(A_f()) << std::endl;
}

本站无任何商业行为
个人在线分享 » 从外部访问类中的私有成员
E-->