/***************************************************************************** *** FILE: typeof.cpp *** DATE: 04/10/2004 *** AUTHOR: Daniel Dementiev *** GOAL: This example illustrates how to find out the type of a managed *** type using the managed operator "__typeof()". *** Please note that the base class 'BattleObject' derived from the *** MS managed class 'Object'. That allows us to use the method *** GetType(). *** To build the project run: *** ...> cl typeof.cpp /clr *****************************************************************************/ #using using namespace System; using namespace System::Collections; __gc class BattleObject : public Object { public: virtual void print() { Console::WriteLine(S"BattleObject"); }; }; __gc class Robot : public BattleObject { public: void print() { Console::WriteLine(S"Robot"); }; }; __gc class Cannon : public BattleObject { public: void print() { Console::WriteLine(S"Cannon"); }; }; void main() { int i; BattleObject *list[] = new BattleObject*[15]; for(i=0;i<15;i++) switch( i%3 ){ case 0: list[i] = new BattleObject(); break; case 1: list[i] = new Robot(); break; case 2: list[i] = new Cannon(); break; } Type *t; for(i=0;iLength;i++){ t = __typeof(Robot); if( t->Equals(list[i]->GetType()) ) list[i]->print(); } Console::WriteLine(S"Object hierarchy:"); for(i=0;i<3;i++){ Type *type = list[i]->GetType(); while( type ){ Console::Write(S" {0} ->", type->Name); type = type->BaseType; } Console::WriteLine(S"\b\b "); } }