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