Skip to content

Instantly share code, notes, and snippets.

@erikformella
Created November 5, 2011 15:37
Show Gist options
  • Save erikformella/1341674 to your computer and use it in GitHub Desktop.
Save erikformella/1341674 to your computer and use it in GitHub Desktop.
showing how virtual functions can make pseudo type introspection
#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