You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
747 B
C++
36 lines
747 B
C++
An example that uses typeid on a polymorphic class hierarchy
|
|
#include <iostream>
|
|
#include <typeinfo>
|
|
using namespace std;
|
|
class Mammal {
|
|
public:
|
|
virtual bool laysEggs() {
|
|
return false;
|
|
}
|
|
};
|
|
class Cat: public Mammal {
|
|
public:
|
|
};
|
|
class Platypus: public Mammal {
|
|
public:
|
|
bool laysEggs() {
|
|
return true;
|
|
}
|
|
};
|
|
int main()
|
|
{
|
|
Mammal *p, AnyMammal;
|
|
Cat cat;
|
|
Platypus platypus;
|
|
p = &AnyMammal;
|
|
cout << "p is pointing to an object of type ";
|
|
cout << typeid(*p).name() << endl;
|
|
p = &cat;
|
|
cout << "p is pointing to an object of type ";
|
|
cout << typeid(*p).name() << endl;
|
|
p = &platypus;
|
|
cout << "p is pointing to an object of type ";
|
|
cout << typeid(*p).name() << endl;
|
|
return 0;
|
|
}
|