Created
March 11, 2014 13:30
-
-
Save mildred/9485620 to your computer and use it in GitHub Desktop.
Break C++ encapsulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A { | |
template<class T> class B {}; | |
template<class T> friend class C; | |
int priv; | |
protected: | |
int pro; | |
}; | |
template<typename T, typename U> | |
class Access : public T { | |
protected: | |
static U& access(T& o) { return static_cast<U&>(o); } | |
}; | |
class D : public Access<A, D> { | |
static int toto() { | |
A a; | |
D &b = static_cast<D&>(a); | |
return b.pro + access(a).pro; | |
} | |
int tata() { | |
A a; | |
return access(a).pro; | |
} | |
}; | |
template<class T> class C { | |
}; | |
namespace { | |
struct X__20 {}; | |
} | |
template<> class A::B<X__20> { | |
static int toto() { | |
A a; | |
return a.priv; | |
} | |
}; | |
template<> class C<X__20> { | |
static int toto() { | |
A a; | |
return a.priv; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment