Created
November 5, 2011 15:37
-
-
Save erikformella/1341674 to your computer and use it in GitHub Desktop.
showing how virtual functions can make pseudo type introspection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
class Animal | |
{ | |
public: | |
virtual void sound() | |
{ | |
} | |
}; | |
class Cat : public Animal | |
{ | |
void sound() | |
{ | |
cout << "meow\n"; | |
} | |
}; | |
class Dog : public Animal | |
{ | |
void sound() | |
{ | |
cout << "woof!\n"; | |
} | |
}; | |
int main() | |
{ | |
vector<Animal*> a; | |
Cat a_cat; | |
Dog a_dog; | |
a.push_back(&a_cat); | |
a.push_back(&a_dog); | |
a[0]->sound(); | |
a[1]->sound(); | |
return 0; | |
} | |
/* | |
outputs | |
meow | |
woof! | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment