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

G

Guest

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,
 
I

ismailp

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

Guest

Hey ismailp,

ismailp said:
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.

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

ismailp

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

Guest

Hey ismailp,

ismailp said:
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);

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

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top