Connecting Tech Pros Worldwide Help | Site Map

Is there 'is' keyword in C++

Tao
Guest
 
Posts: n/a
#1: Mar 15 '07
hi. Group,

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



Tamas Demjen
Guest
 
Posts: n/a
#2: Mar 15 '07

re: Is there 'is' keyword in C++


Tao wrote:
Quote:
C# has a keyword "is" to determine if a object is a given type. Does C++.NET
has something simliar? thanks.
dynamic_cast. See:
http://tinyurl.com/2k4sld

Tom
Vincent Finn
Guest
 
Posts: n/a
#3: Mar 15 '07

re: Is there 'is' keyword in C++


hi. Group,
Quote:
>
C# has a keyword "is" to determine if a object is a given type. Does
C++.NET has something simliar? thanks.
>
Hi,

Use dynamic_cast.

The MSDN suggests a wrapper
http://msdn2.microsoft.com/en-us/lib...e9(vs.80).aspx

Vin


=?Utf-8?B?RGF2aWQgQW50b24=?=
Guest
 
Posts: n/a
#4: Mar 15 '07

re: Is there 'is' keyword in C++


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" wrote:
Quote:
hi. Group,
>
C# has a keyword "is" to determine if a object is a given type. Does C++.NET
has something simliar? thanks.
>
>
>
>
Ben Voigt
Guest
 
Posts: n/a
#5: Mar 15 '07

re: Is there 'is' keyword in C++



"Tao" <ttliu2000@hotmail.comwrote in message
news:%23170msxZHHA.808@TK2MSFTNGP04.phx.gbl...
Quote:
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".


Duncan Smith
Guest
 
Posts: n/a
#6: Mar 17 '07

re: Is there 'is' keyword in C++


On Mar 15, 4:01 pm, "Tao" <ttliu2...@hotmail.comwrote:
Quote:
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.

Duncan Smith
Guest
 
Posts: n/a
#7: Mar 17 '07

re: Is there 'is' keyword in C++


On Mar 17, 3:00 am, "Duncan Smith" <DSmith1...@googlemail.comwrote:
Quote:
On Mar 15, 4:01 pm, "Tao" <ttliu2...@hotmail.comwrote:
>
Quote:
hi. Group,
>
Quote:
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.
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?

Carl Daniel [VC++ MVP]
Guest
 
Posts: n/a
#8: Mar 17 '07

re: Is there 'is' keyword in C++


Duncan Smith wrote:
Quote:
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


Closed Thread