a while ago stumbled upon a neat template trick to do this on this blog: http://bloglitb.blogspot.com/2010/07/access-to-private-members-thats-easy.html
Do NOT use this in any production code, it is just a educational example !!!
it takes basically leverage of the "private" being ignored on some part of template initialization
template<typename Tag>
struct result {
/* export it ... */
typedef typename Tag::type type;
static type ptr;
};
template<typename Tag>
typename result<Tag>::type result<Tag>::ptr;
template<typename Tag, typename Tag::type p>
struct rob : result<Tag> {
/* fill it ... */
struct filler {
filler() { result<Tag>::ptr = p; }
};
static filler filler_obj;
};
template<typename Tag, typename Tag::type p>
typename rob<Tag, p>::filler rob<Tag, p>::filler_obj;
usage: take following struct:
struct A {
private:
void f() {
std::cout << "proof!" << std::endl;
}
};
create your "robber"
struct Af { typedef void(A::*type)(); };
template class rob<Af, &A::f>;
use it:
int main()
{
A a;
(a.*result<Af>::ptr)();
return 0;
}