Commit d2e095ca994667cfb6810ef2a8f4dbff93b5d29d
1 parent
b7d3599e
Added satellite.cpp with example of layout of complex class hierarchy
Showing
1 changed file
with
76 additions
and
0 deletions
examples10/satellite.cpp
0 → 100644
1 | +#include <iostream> | ||
2 | +using namespace std; | ||
3 | + | ||
4 | +struct Task | ||
5 | +{ | ||
6 | + int next_event_time; | ||
7 | + void execute(){}; | ||
8 | + virtual void get_debug(){ cout << "Task" << endl;}; | ||
9 | + virtual int f(int){}; | ||
10 | +}; | ||
11 | + | ||
12 | +struct Displayed | ||
13 | +{ | ||
14 | + int x; | ||
15 | + int y; | ||
16 | + int z; | ||
17 | + int rotx; | ||
18 | + int roty; | ||
19 | + int rotz; | ||
20 | + virtual void draw(){}; | ||
21 | + virtual void get_debug(){cout << "Displayed" << endl;}; | ||
22 | + double f(double){}; | ||
23 | +}; | ||
24 | + | ||
25 | + | ||
26 | +struct Satellite : public Task, public Displayed | ||
27 | +{ | ||
28 | + double uplink_frequency; | ||
29 | + double downlink_frequency; | ||
30 | + void transmit(); | ||
31 | + using Displayed::f; | ||
32 | + using Task::f; | ||
33 | + virtual void get_debug(){cout << "Satellite" << endl;}; | ||
34 | +}; | ||
35 | + | ||
36 | + | ||
37 | +struct Test { | ||
38 | + int a; | ||
39 | + double b; | ||
40 | +}; | ||
41 | + | ||
42 | + | ||
43 | +int main() | ||
44 | +{ | ||
45 | + cout << sizeof(Task) << endl; | ||
46 | + cout << sizeof(Displayed) << endl; | ||
47 | + cout << sizeof(Satellite) << endl; | ||
48 | + | ||
49 | + Satellite s; | ||
50 | + Satellite* sptr = &s; | ||
51 | + | ||
52 | + cout << (char*)(&(sptr->next_event_time)) - (char*)sptr << endl; | ||
53 | + cout << (char*)(&(sptr->x)) - (char*)sptr << endl; | ||
54 | + cout << (char*)(&(sptr->rotz)) - (char*)sptr << endl; | ||
55 | + cout << (char*)(&(sptr->uplink_frequency)) - (char*)sptr << endl; | ||
56 | + | ||
57 | + | ||
58 | + cout << offsetof(Test, b) << endl; | ||
59 | + | ||
60 | + Task* tptr = sptr; | ||
61 | + Displayed* dptr = sptr; | ||
62 | + | ||
63 | + cout << sptr << "," << tptr << "," << dptr << endl; | ||
64 | + | ||
65 | + return 1; | ||
66 | + | ||
67 | + tptr -> get_debug(); | ||
68 | + dptr -> get_debug(); | ||
69 | + | ||
70 | + sptr -> get_debug(); | ||
71 | + | ||
72 | + sptr -> f(1); | ||
73 | + return 0; | ||
74 | +} | ||
75 | + | ||
76 | + |