473,499 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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,
--
Tom Tempelaere.
Nov 17 '05 #1
5 9684
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.

Nov 17 '05 #2
Hey ismailp,

"ismailp" wrote:
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.
Nov 17 '05 #3
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.

Nov 17 '05 #4
Hey ismailp,

"ismailp" wrote:
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.
Nov 17 '05 #5
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
"TT (Tom Tempelaere)" wrote:
Hey ismailp,

"ismailp" wrote:
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.

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1726
by: hazz | last post by:
I have inherited code but don't know exactly what this one line of code is doing? What it is the purpose of (ClassFileName)ClassFileName......... > ClassFileName vs2 = (ClassFileName...
3
3827
by: Ole Hanson | last post by:
Hi Trying to cast an exception to the correct type, I encounter some "strange" experiences: The compiler complains that the variable "t" is unknown in line 2 (Exception trueException =...
17
2659
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
3
21198
by: mra | last post by:
I want to cast an object that I have created from a typename to the corresponding type. Can anycone tell me how to do this? Example: //Here, Create the object of type "MyClass" object...
3
2198
by: aljamala | last post by:
Hello, I have the following method to help me in installing some COM components onto the machine...below is a snippet that is causing the problem... Type objType = null; COMAdminCatalog...
9
6369
by: Jim in Arizona | last post by:
I get this error: Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputText' to type 'System.Web.UI.WebControls.TextBox'. Using this code: Dim test3 As TextBox test3 =...
5
2623
by: Eric bouxirot | last post by:
hi, i'd like to do a "cast", who seem to be very complicated in .NET. in C standard it's so easy... i explain : i have made an app based on plugin architecture in VB.NET. all work fine.. ...
1
2321
by: =?Utf-8?B?U2NvdHQ=?= | last post by:
Hello, Using VS2008 in a C# web service application, a class has been created that inherits from the ConfigurationSelection. This class file has been placed in the App_Code folder. The...
20
2863
by: raylopez99 | last post by:
Inspired by Chapter 8 of Albahari's excellent C#3.0 in a Nutshell (this book is amazing, you must get it if you have to buy but one C# book) as well as Appendix A of Jon Skeet's book, I am going...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.