Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a C++/CLI ref class and a C# class

Tags:

c#

c++-cli

I had read that the C++/CLI ref class was the equivalent to a C# class. However, I'm seeing a difference between this in each class.

EDIT: I obtained this from Visual Studio 2010's locals. (Also, I noted a difference at compile time when doing exact same cast - the C++/CLI class gave an error stating it could not convert Object^ to MyClass^, whereas casting with the C# class creates no errors at compile time).

C# class:

namespace MyNamespace
{

   public class MyClass
   {    
       public MyClass()
           {
               //Do something
           }
   }
}

this = MyNamespace.MyClass (What I expect and want!)


C++/CLI ref class:

namespace MyNamespace
{

   public ref class MyClass
   {    
       public:
           MyClass::MyClass()
           {
               //Do something
           }
   }
}

this = System::Object^ (Not what I expected or want!!)


I would expect this to be like the C# example, which states that this is of type MyClass. However, the C++/CLI ref class states it is of type Object.. Which is definitely NOT what I want. (This makes it difficult in various situations, such as casting in a C++ function).

So my question is (two part):

a) why are these classes behaving differently with this?

b) how do I get my C++/CLI ref class to have the correct type in this??

Thanks for your time in advance!

like image 867
developer Avatar asked Dec 31 '25 20:12

developer


1 Answers

What you are seeing in the debugger is the name of the base class - in this case Object^. Change the base class of MyClass to something else and you will see what I mean.

Locals window

Note that the right most column displays the type.

like image 179
Justin Avatar answered Jan 02 '26 11:01

Justin