1 | #ifdef DO_VIRTUAL_INHERITANCE |
2 | #define VIRTUAL virtual |
3 | #else |
4 | #define VIRTUAL |
5 | #endif |
6 | |
7 | #include <stdio.h> |
8 | |
9 | class Base |
10 | { |
11 | public: |
12 | Base(int val) : m_base_val (val) {} |
13 | virtual ~Base() {} |
14 | |
15 | virtual void |
16 | forcast(int input) { |
17 | int future_val = m_base_val + input * 1; |
18 | printf(format: "Forcasting %d\n" , future_val); |
19 | } |
20 | |
21 | protected: |
22 | int m_base_val; |
23 | }; |
24 | |
25 | class DerivedA : public VIRTUAL Base |
26 | { |
27 | public: |
28 | DerivedA(int val) : Base(val*2), m_a_val(val) { |
29 | printf(format: "DerivedA::ctor()->\n" ); |
30 | printf(format: "m_base_val=%d\n" , m_base_val); |
31 | printf(format: "m_a_val=%d\n" , m_a_val); |
32 | } |
33 | virtual ~DerivedA() {} |
34 | |
35 | private: |
36 | int m_a_val; |
37 | }; |
38 | |
39 | class DerivedB : public VIRTUAL Base |
40 | { |
41 | public: |
42 | DerivedB(int val) : Base(val), m_b_val(val*3) { |
43 | printf(format: "DerivedB::ctor()->\n" ); |
44 | printf(format: "m_base_val=%d\n" , m_base_val); |
45 | printf(format: "m_b_val=%d\n" , m_b_val); |
46 | } |
47 | virtual ~DerivedB() {} |
48 | |
49 | virtual void |
50 | forcast(int input) { |
51 | int future_val = m_b_val + input * 2; |
52 | printf(format: "Forcasting %d\n" , future_val); |
53 | } |
54 | |
55 | private: |
56 | int m_b_val; |
57 | }; |
58 | |
59 | int |
60 | main(int argc, char **argv) |
61 | { |
62 | DerivedA* dA = new DerivedA(10); |
63 | DerivedB* dB = new DerivedB(12); |
64 | Base *array[2] = {dA, dB}; |
65 | Base *teller = NULL; |
66 | for (int i = 0; i < 2; ++i) { |
67 | teller = array[i]; |
68 | teller->forcast(input: i); // Set breakpoint here. |
69 | } |
70 | |
71 | return 0; |
72 | } |
73 | |