Skip to content

Latest commit

 

History

History
187 lines (126 loc) · 4.31 KB

typeid-cpp-component-extensions.md

File metadata and controls

187 lines (126 loc) · 4.31 KB
descriptiontitlems.datems.topichelpviewer_keywordsms.assetid
Learn more about: typeid (C++/CLI and C++/CX)
typeid (C++/CLI and C++/CX)
10/12/2018
reference
typeid keyword [C++]
e9706cae-e7c4-4d6d-b474-646d73df3e70

typeid (C++/CLI and C++/CX)

Gets a value that indicates the type of an object.

Note

This topic refers to the C++ Component Extensions version of typeid. For the ISO C++ version of this keyword, see typeid Operator.

All Runtimes

Syntax

T::typeid

Parameters

T
A type name.

Windows Runtime

Syntax

Platform::Type^ type = T::typeid;

Parameters

T
A type name.

Remarks

In C++/CX, typeid returns a Platform::Type that is constructed from runtime type information.

Requirements

Compiler option: /ZW

Common Language Runtime

Syntax

System::Type^ type = T::typeid; 

Parameters

type
The name of a type (abstract declarator) for which you want the System::Type object.

Remarks

typeid is used to get the xref:System.Type for a type at compile time.

typeid is similar to getting the System::Type for a type at run time using xref:System.Type.GetType%2A or xref:System.Object.GetType%2A. However, typeid only accepts a type name as a parameter. If you want to use an instance of a type to get its System::Type name, use GetType.

typeid must be able to evaluate a type name (type) at compile time, whereas GetType evaluates the type to return at run time.

typeid can take a native type name or common language runtime alias for the native type name; see .NET Framework Equivalents to C++ Native Types (C++/CLI) for more information.

typeid also works with native types, although it will still return a System::Type. To get a type_info structure, use typeid Operator.

Requirements

Compiler option: /clr

Examples

The following example compares the typeid keyword to the GetType() member.

// keyword__typeid.cpp// compile with: /clrusingnamespaceSystem; ref structG { int i; }; intmain() { G ^ pG = gcnew G; Type ^ pType = pG->GetType(); Type ^ pType2 = G::typeid; if (pType == pType2) Console::WriteLine("typeid and GetType returned the same System::Type"); Console::WriteLine(G::typeid); typedeffloat* FloatPtr; Console::WriteLine(FloatPtr::typeid); }
typeid and GetType returned the same System::Type G System.Single* 

The following sample shows that a variable of type System::Type can be used to get the attributes on a type. It also shows that for some types, you will have to create a typedef to use typeid.

// keyword__typeid_2.cpp// compile with: /clrusingnamespaceSystem;usingnamespaceSystem::Security;usingnamespaceSystem::Security::Permissions;typedefint ^ handle_to_int; typedefint * pointer_to_int; public ref classMyClass {}; classMyClass2 {}; [attribute(AttributeTargets::All)] ref classAtClass { public:AtClass(Type ^) { Console::WriteLine("in AtClass Type ^ constructor"); } }; [attribute(AttributeTargets::All)] ref classAtClass2 { public:AtClass2() { Console::WriteLine("in AtClass2 constructor"); } }; // Apply the AtClass and AtClass2 attributes to class B [AtClass(MyClass::typeid), AtClass2] [AttributeUsage(AttributeTargets::All)] ref classB : Attribute {}; intmain() { Type ^ MyType = B::typeid; Console::WriteLine(MyType->IsClass); array<Object^>^ MyArray = MyType -> GetCustomAttributes(true); for (int i = 0 ; i < MyArray->Length ; i++ ) Console::WriteLine(MyArray[i]); if (int::typeid != pointer_to_int::typeid) Console::WriteLine("int::typeid != pointer_to_int::typeid, as expected"); if (int::typeid == handle_to_int::typeid) Console::WriteLine("int::typeid == handle_to_int::typeid, as expected"); }
True in AtClass2 constructor in AtClass Type ^ constructor AtClass2 System.AttributeUsageAttribute AtClass int::typeid != pointer_to_int::typeid, as expected int::typeid == handle_to_int::typeid, as expected 

See also

Component Extensions for .NET and UWP

close