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