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++ / March 2005

Tip: Looking for answers? Try searching our database.

How to cast from Object to managed reference type in C++.NET

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
TT (Tom Tempelaere) - 08 Mar 2005 14:15 GMT
Hey everyone,

I know regular c++ has cast operators: dynamic_cast, static_cast, const_cast
and reinterpret_cast.

But, I'd like to know if I can use any of these to cast from System::Object
to a managed class. If not, what cast operator do I need?

Thanks,
Signature

Tom Tempelaere.

ismailp - 08 Mar 2005 14:29 GMT
yes, you can.

ref class A
{
public:
// class members
};

you can do following:

A ^pa = gcnew A;
Object^ po = pa;
A^ pa2 = static_cast<A^>(po);
A^ pa3 = (A^)o;

use static_cast for convenience.
TT (Tom Tempelaere) - 08 Mar 2005 14:47 GMT
Hey ismailp,

> yes, you can.
>
[quoted text clipped - 12 lines]
>
> use static_cast for convenience.

Mmm, I don't know anything about the use of the ^ symbol in managed C++.
Could you elaborate please?

What I do to declare a new class:

 public __sealed __gc class SomeClass : public SomeBase
 {
    // ...
 };

What I do now to cast from Object to SomeBase:

 Object* someClass = new SomeClass();
 SomeBase* someBase = __try_cast<SomeBase*>( someClass );

You seem to use any cast (C-style any cast, and C++ static_cast).

Last: What is gcnew? I can just use new, right?

Thank you,
Tom Tempelaere.
ismailp - 08 Mar 2005 14:58 GMT
oops, sorry, I thought you are talking about C++/CLI.

ok on managed c++, same things apply:

SomeClass* pcls = new SomeClass;
Object* po = pcls;
SomeClass* pcls2 = static_cast<SomeClass*>(po);
SomeClass* pcls3 = __try_cast<SomeClass*>(po);
SomeClass* pcls4 = dynamic_cast<SomeClass*>(po);

C style casting, however, may cause a warning. Prefer static_cast.

gcnew is C++/CLI version of new. I thought you are on C++/CLI - our new
language.
TT (Tom Tempelaere) - 08 Mar 2005 16:21 GMT
Hey ismailp,

> oops, sorry, I thought you are talking about C++/CLI.
>
[quoted text clipped - 5 lines]
> SomeClass* pcls3 = __try_cast<SomeClass*>(po);
> SomeClass* pcls4 = dynamic_cast<SomeClass*>(po);

So what is the effective difference between all the casts? From what I've
read I understand that __try_cast throws an InvalidCastException, just as
with C#-casting. But what is the difference with static_cast and
dynamic_cast? I know what these do in C++, but I don't see the need for them
in managed languages. Can I assume they behave similarly as in regular C++?

> C style casting, however, may cause a warning. Prefer static_cast.

> gcnew is C++/CLI version of new. I thought you are on C++/CLI - our new
> language.

I have never heard of C++/CLI. I guess that is the same as managed C++, only
without the option to add unmanaged C++ code? And some conveniency
features...?

Thank you,
Tom Tempelaere.
George Jackson - 21 Mar 2005 17:17 GMT
static_cast and dynamic_cast works the same way as in C++. For example, you
can use static_cast to unbox a value type:

   Int32 x = 25;

   __box Int32 *bx = __box(x); // Boxing
   Int32 y = *bx; // Unboxing: static_cast not needed here
   
   Object *obj = __box(x); // Boxing
   Int32 z = *static_cast<__box Int32*>(obj); // Unboxing

George

> Hey ismailp,
>
[quoted text clipped - 25 lines]
> Thank you,
> Tom Tempelaere.

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.