Is there 'is' keyword in C++

  • Thread starter Thread starter Tao
  • Start date Start date
T

Tao

hi. Group,

C# has a keyword "is" to determine if a object is a given type. Does C++.NET
has something simliar? thanks.
 
As the others have indicated, it is dynamic_cast. Here's an example to
clarify:
The following C# code:
if (someObject is SomeType)
translates to C++/CLI:
if (dynamic_cast<SomeType^>(someObject) != nullptr)
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: C#/VB to Python converter
 
Tao said:
hi. Group,

C# has a keyword "is" to determine if a object is a given type. Does
C++.NET has something simliar? thanks.

"is" was a mistake in C#, made necessary due to lack of generics. The "as"
keyword should be changed to support value types using Nullable<>, and then
there would be no more need for is. After all, what is the point of "is",
except to find out if a subsequent cast will succeed. "as" combines both
operations and is much more efficient. C++, with dynamic_cast and allowing
tracking handles to value types (essentially providing Nullable<>), totally
obviates the need for "is".
 
hi. Group,

C# has a keyword "is" to determine if a object is a given type. Does C++.NET
has something simliar? thanks.

Using C++ run-time type information you can try and cast the object to
an object pointer of the desired type using dynamic_cast<>. If the
resulting pointer is not NULL then the object was (or was derived
from) the specified type.

If you're using MFC there's also the IsKindOf and IsDerivedFrom macros
- provided your class derives from CObject.

Finally, if you want to keep things really simple you could add an
enum member variable to the base class, and assign it's value in the
constructor. Derived classes would assign a different value depending
upon their type, but you can always cast any object to the base class
and call a member function which would return the enum.

i.e (roughly).

enum tagType
{
kBase,
kB,
kC
}

class A
{
A(tag eType) { m_eType = eType; }
A() { m_eType = kBase; }

tagType GetType { return m_eType; }
};

class B : A
{
B() { m_eTyoe = kB; }
};

HTH,

Duncan.
 
Using C++ run-time type information you can try and cast the object to
an object pointer of the desired type using dynamic_cast<>. If the
resulting pointer is not NULL then the object was (or was derived
from) the specified type.

If you're using MFC there's also the IsKindOf and IsDerivedFrom macros
- provided your class derives from CObject.

Finally, if you want to keep things really simple you could add an
enum member variable to the base class, and assign it's value in the
constructor. Derived classes would assign a different value depending
upon their type, but you can always cast any object to the base class
and call a member function which would return the enum.

i.e (roughly).

enum tagType
{
kBase,
kB,
kC

}

class A
{
A(tag eType) { m_eType = eType; }
A() { m_eType = kBase; }

tagType GetType { return m_eType; }

};

class B : A
{
B() { m_eTyoe = kB; }

};

HTH,

Duncan.

If it's a managed class, there's maybe something you can do with the
CLR and reflection to get type info from attributes? But I'm not too
'up' on that at the moment.. anybody know?
 
Duncan said:
If it's a managed class, there's maybe something you can do with the
CLR and reflection to get type info from attributes? But I'm not too
'up' on that at the moment.. anybody know?

Just use dynamic_cast. Under managed C++, it's compiled to exactly the same
IL as the C# 'as' operator.

-cd
 
Back
Top