forked from singhnir/C-Plus-Plus-Robert-Lafore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Typeid.cpp
executable file
·32 lines (31 loc) · 922 Bytes
/
Typeid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// typeid.cpp
// demonstrates typeid() function
// RTTI must be enabled in compiler
#include <iostream>
#include <typeinfo> //for typeid()
using namespace std;
////////////////////////////////////////////////////////////////
class Base
{
virtual void virtFunc() //needed for typeid
{ }
};
class Derv1 : public Base
{ };
class Derv2 : public Base
{ };
////////////////////////////////////////////////////////////////
void displayName(Base* pB)
{
cout << "pointer to an object of: "; //display name of class
cout << typeid(*pB).name() << endl; //pointed to by pB
}
//--------------------------------------------------------------
int main()
{
Base* pBase = new Derv1;
displayName(pBase); //"pointer to an object of class Derv1"
pBase = new Derv2;
displayName(pBase); //"pointer to an object of class Derv2"
return 0;
}