Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2005

Tip: Looking for answers? Try searching our database.

C++/CLI equivalent to as keyword?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
karch - 25 Oct 2005 08:12 GMT
How would this C# contruct be represented in C++/CLI? Or is it even
possible?

PolicyLevel level = (enumerator->Current) as PolicyLevel;

Thanks,
Karch
adebaene@club-internet.fr - 25 Oct 2005 08:37 GMT
> How would this C# contruct be represented in C++/CLI? Or is it even
> possible?
>
> PolicyLevel level = (enumerator->Current) as PolicyLevel;

PolicyLevel* level=dynamic_cast<PolicyLevel*>(enumerator->Current);

The equivalent of a cast in C# is __try_cast.

Arnaud
MVP - VC
bonk - 25 Oct 2005 11:12 GMT
adebaene@club-internet.fr schrieb:

>>How would this C# contruct be represented in C++/CLI? Or is it even
>>possible?
[quoted text clipped - 7 lines]
> Arnaud
> MVP - VC

is __try_cast c++/CLI or managed c++ ? Or both ?
adebaene@club-internet.fr - 25 Oct 2005 12:37 GMT
> is __try_cast c++/CLI or managed c++ ? Or both ?

Managed C++. For C++/CLI, use safe_cast : Check MSDN for details
(a.k.a. RTFM)

Arnaud
MVP - VC
David Anton - 25 Oct 2005 14:03 GMT
dynamic_cast is the equivalent to the C# "as".

static_cast is the equivalent in 2003 to basic C# casting (__try_cast is
strictly 2003, but is more of a development tool, not a production casting
technique).

safe_cast is the equivalent to basic C# casting in CLI.

Signature

David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter

> > How would this C# contruct be represented in C++/CLI? Or is it even
> > possible?
[quoted text clipped - 7 lines]
> Arnaud
> MVP - VC
adebaene@club-internet.fr - 25 Oct 2005 15:49 GMT
> dynamic_cast is the equivalent to the C# "as".
>
> static_cast is the equivalent in 2003 to basic C# casting (__try_cast is
> strictly 2003, but is more of a development tool, not a production casting
> technique).

Why a "developement tool"? It gives you exactly the same behaviour as
C# casting, whereas static_cast result is undefined if the cast is
invalid. static_cast documentation for MC++ states explictely :
"Unchecked pointer casts can break the type safety of __gc pointers,
and cause the garbage collector to fail."

Arnaud
MVP - VC
David Anton - 25 Oct 2005 17:00 GMT
In 'Managed C++ and .NET Development', Stephen Fraser gives a good
explanation for why you should regard __try_cast as a development tool -
basically, __try_cast exceptions will only occur if there are actual mistakes
in your code.  But perhaps this is also close to what C# does.
Signature

David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter

> > dynamic_cast is the equivalent to the C# "as".
> >
[quoted text clipped - 10 lines]
> Arnaud
> MVP - VC
Arnaud Debaene - 25 Oct 2005 19:13 GMT
> In 'Managed C++ and .NET Development', Stephen Fraser gives a good
> explanation for why you should regard __try_cast as a development
> tool - basically, __try_cast exceptions will only occur if there are
> actual mistakes in your code.
We agree; that's the wole point : It's better to have an exception that
unmanaged behaviour and GC failure. Of course, it's still better to have an
error at build time, but you don't always have what you want ;-)

Now, if you are *really* performance worried (as much C++ developpers are,
generally without any measurement to prove that their worries are justified
;-), you can replace __try_cast by static_cast and *pray* not to have an
unnoticed invalid cast that will break hell loose in front of the client.

>  But perhaps this is also close to what
> C# does.
AFAIK, __try_cast and C# casts do strictly the same thing.

Arnaud
MVP - VC
David Anton - 25 Oct 2005 20:40 GMT
Good point Arnaud.
It's apparent that we need to change conversion of the basic C# cast to use
__try_cast for the 2003 target.  (It's in today's build).

Our conversion to the CLI target is correct in using safe_cast.
Signature

David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter

> > In 'Managed C++ and .NET Development', Stephen Fraser gives a good
> > explanation for why you should regard __try_cast as a development
[quoted text clipped - 15 lines]
> Arnaud
> MVP - VC
Willy Denoyette [MVP] - 25 Oct 2005 21:00 GMT
>> In 'Managed C++ and .NET Development', Stephen Fraser gives a good
>> explanation for why you should regard __try_cast as a development
[quoted text clipped - 16 lines]
> Arnaud
> MVP - VC

__try_cast (MC++)  is recast into safe_cast ( C++/CLI) , both generate
exactly the same IL instruction (castclass) just like an ordinary cast in
C#.

Willy.
Nishant Sivakumar - 25 Oct 2005 15:19 GMT
See : - How to: Implement is and as C# Keywords in C++
http://msdn2.microsoft.com/en-us/library/85af44e9

Signature

Regards,
Nish [VC++ MVP]

> How would this C# contruct be represented in C++/CLI? Or is it even
> possible?
[quoted text clipped - 3 lines]
> Thanks,
> Karch

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.