-
Notifications
You must be signed in to change notification settings - Fork 82
/
test_private_member.cpp
57 lines (46 loc) · 1.32 KB
/
test_private_member.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//http://www.purecpp.cn/detail?id=2372
//g++ prog.cc -Wall -Wextra -std=c++17 -I../src
#include <tuple>
#include <string>
#include <iostream>
#include "stub.h"
template <typename T, auto... field>
struct ThiefMember {
friend auto steal_impl(T&) {
return std::make_tuple(field...);
}
};
class Bank_t{
int id;
std::string name;
std::string money(){
return "100$";
}
public:
Bank_t(int i, std::string str) : id(i), name(str){}
void set_id(int i){id = i;}
};
auto steal_impl(Bank_t& t);
template struct ThiefMember<Bank_t, &Bank_t::id, &Bank_t::name, &Bank_t::money>;
std::string money_stub(void *)
{
return "10000$";
}
int main(void) {
Bank_t bank(1, "ok");
auto tp = steal_impl(bank);
auto& id = bank.*(std::get<0>(tp)); // 1
auto name = bank.*(std::get<1>(tp)); //ok
auto money = (bank.*(std::get<2>(tp)))(); // 100$
std::cout << "id: " << id << std::endl;
std::cout << "name: " << name << std::endl;
std::cout << "money: " << money << std::endl;
bank.set_id(2);
std::cout << "id: " << id << std::endl;
Stub stub;
auto money_ptr = (void*)(bank.*(std::get<2>(tp)));
stub.set(money_ptr, money_stub);
auto money_stub = (bank.*(std::get<2>(tp)))(); // 10000$
std::cout << "money_stub: " << money_stub << std::endl;
return 0;
}