Typeid

Typeid

In C++, the typeid keyword is used to determine the class of an object at runtime. According to the C++ specification, it returns a reference to type_info. The use of typeid is often preferred over dynamic_cast<"class_type"> in situations where just the class information is needed, because typeid is a constant-time procedure, whereas dynamic cast must traverse the class derivation lattice of its argument at runtime.

Objects of class bad_typeid are thrown when typeid is called on a dereferenced null pointer. The class is derived from exception, and thrown by typeid and others.

Note that it is only useful to use typeid on the dereference of a pointer or reference (i.e. typeid(*ptr) or typeid(ref)) to a polymorphic class (a class with at least one virtual method). This is because these are the only expressions that are associated with run-time type information. The type of any other expression is statically known at compile time, and so is not very interesting.

Caution: the actual text returned by typeid is platform specific so don't rely on it if attempting to produce platform independent code.

Example


#include
#include //for 'typeid' to workusing namespace std;

class Person {public: // ... Person members ... virtual ~Person() {;

class Employee : public Person { // ... Employee members ...};

int main (){ Person person; Employee employee; Person *ptr = &employee;

cout << typeid(person).name() << endl; // Person (statically known at compile-time) cout << typeid(employee).name() << endl; // Employee (statically known at compile-time) cout << typeid(ptr).name() << endl; // Person * (statically known at compile-time) cout << typeid(*ptr).name() << endl; // Employee (looked up dynamically at run-time // because it is the dereference of a pointer to a polymorphic class) return 0;}

Output (exact output varies by system):

Person Employee *Person Employee

See also

* Run-time type information


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать реферат

Look at other dictionaries:

  • History of the Jews in Croatia — The Jewish community of Croatia dates back to at least the third century AD, although little is known of the community until the tenth and fifteenth centuries. The community, over 20,000 strong on the eve of World War II, was almost entirely… …   Wikipedia

  • Removal from the Order of Canada — Recipients of the Order of Canada can have their honour revoked if the Order s Advisory Council feels that a recipient s actions have brought dishonour to the Order. As of 2005, only two people have been removed from the Order of Canada: Alan… …   Wikipedia

  • Citizens for Pennsylvania's Future — Established 1998 Exec. Dir. Rev. K. Joy Kauffman President Ms. Jan Jarrett …   Wikipedia

  • Type introspection — In computing, type introspection is a capability of some object oriented programming languages to determine the type of an object at runtime. This is a notable capability of the Objective C language, and is a common feature in any language that… …   Wikipedia

  • Operators in C and C++ — This is a list of operators in the C and C++ programming languages. All the operators listed exist in C++; the fourth column Included in C , dictates whether an operator is also present in C. Note that C does not support operator overloading.… …   Wikipedia

  • History of the Jews in the Republic of Macedonia — The history of Jews in the territory of the present day Republic of Macedonia began in Roman times, when Jews first arrived in the region in the first century BC. Today, no more than 200 Jews reside in the Republic of Macedonia, almost all in the …   Wikipedia

  • Ypres Town Commonwealth War Graves Commission Cemetery and Extension — Infobox Military Cemetery name= Ypres Town body= Commonwealth War Graves Commission use dates= 1914 1915, 1918 and 1940 established= 1914 designer= Sir Reginald Blomfield coordinates= coord|50|51|12.3|N|02|53|53.5|E| nearest town= Ypres, West… …   Wikipedia

  • Clanwilliam (County Limerick) — Clanwilliam (Irish: Clann Liam[1]) is a barony in County Limerick in Ireland.[1] According to Patrick Weston Joyce, the name Clanwilliam derives from the descendants (clan) of William de Burgh; similarly for the Tipperary barony of… …   Wikipedia

  • University of Saskatchewan — ] In many fields Biologist Walter P. Thompson leadership brought innovation, insight and research to new areas beginning with rust resistant varieties of wheat which curtailed the 1916 catastrophic outbreak of rust. He also was instrumental in… …   Wikipedia

  • Data Transformation Services — Data Transformation Services, or DTS, is a set of objects and utilities to allow the automation of extract, transform and load operations to or from a database. The objects are DTS packages and their components, and the utilities are called DTS… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”