-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcls.cpp
More file actions
71 lines (52 loc) · 1.37 KB
/
cls.cpp
File metadata and controls
71 lines (52 loc) · 1.37 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
using namespace std;
struct Cls {
Cls(char c, double d, int i) : c(c), d(d), i(i) {};
private:
char c;
double d;
int i;
};
// Use this function to get values position
void list(Cls &cls) {
Cls * obj = &cls;
char * p_obj = (char *) obj;
int size = sizeof(cls);
for (int i = 0; i < size / 4; i++) {
char * p_i = p_obj + i;
cout << "byte in object: " << i << " value: " << * p_i << '\n';
}
}
char &get_c(Cls &cls) {
Cls * obj = &cls;
char * p_obj = (char *) obj;
char & r_c = * p_obj;
return r_c;
}
double &get_d(Cls &cls) {
Cls * obj = &cls;
double * p_obj = (double *) obj;
double * p_d = p_obj + 1;
double & r_d = * p_d;
return r_d;
}
int &get_i(Cls &cls) {
Cls * obj = &cls;
int * p_obj = (int *) obj;
int * p_i = p_obj + 4;
int & r_i = * p_i;
return r_i;
}
int main() {
Cls a('x', 3.14, 759);
cout << "sizes: " << sizeof('x') << ' ' << sizeof(3.14) << ' ' << sizeof(1) << " struct: " << sizeof(a) << '\n';
char r_c = get_c(a);
cout << r_c << '\n';
char & c_value = get_c(a);
cout << "c value: " << c_value << '\n';
int & i_value = get_i(a);
cout << "i value: " << i_value << '\n';
double & d_value = get_d(a);
cout << "d value: " << d_value << '\n';
list(a);
}