1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #include <cstdio> 17 18 class Foo 19 { 20 public: 21 virtual ~Foo() { } 22 virtual void print() 23 { 24 std::printf("in Foo!\n"); 25 } 26 }; 27 28 class Bar: public Foo 29 { 30 public: 31 void print() 32 { 33 std::printf("in Bar!\n"); 34 } 35 }; 36 37 int main() 38 { 39 Foo* foo = new Bar(); 40 Bar* bar; 41 42 bar = dynamic_cast<Bar*>(foo); 43 if (bar != NULL) { 44 printf("OK: 'foo' is pointing to a Bar class instance.\n"); 45 } else { 46 fprintf(stderr, "KO: Could not dynamically cast 'foo' to a 'Bar*'\n"); 47 return 1; 48 } 49 50 delete foo; 51 return 0; 52 } 53