-
Notifications
You must be signed in to change notification settings - Fork 4
/
stealing_private2.cpp
42 lines (34 loc) · 1.06 KB
/
stealing_private2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Stealing private members, take 2, simplified with explanations
// See stealing_private1.cpp for a more general solutions
// The code is adapted from http://cpp.kjx.cz/private_backdoor.html
//
// See also:
// https://stackoverflow.com/q/424104/3093378
// https://stackoverflow.com/questions/12993219/access-private-member-using-template-trick
//
// And also Overload 156 (pg. 22)
// https://members.accu.org/index.php/journals/2776
#include <iostream>
class Foo {
public:
int getx() const { return _x; }
private:
int _x{42};
};
// access rules are not enforced in templates
// so circumvent the access rules by using a pointer-to-member
template <int Foo::*mem_ptr>
struct Thief {
friend int& steal(Foo& foo) { return foo.*mem_ptr; }
};
// make the Thief friend function visible outside
int& steal(Foo& foo);
// implicit instantiation which in effect defines int& steal(Foo& foo);
template struct Thief<&Foo::_x>;
int main() {
Foo foo;
std::cout << foo.getx() << '\n';
steal(foo) = 100;
// bam, proof:
std::cout << foo.getx() << '\n';
}