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.
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.