what is the difference between
Class1 class;
and
Class1 class = gcnew Class1();
in C++/CLI. How do they differ in their implementation?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The first instantiates an object on the stack in unmanaged code space. This is standard c/c++ behaviour. The compiler know how bug the object is in bytes and moves the stack pointer in the curent function to ‘allocate’ that memory. It will then call the constructor for the class.
The latter instantiates an object on the CLR Managed Heap, and is a feature of the Managed C++. Here the object is created and managed within the CLR. Think of the CLR as similar to the Java Virtual Machine.
There is too much to explain here but you need to understand .net (see this tutorial), and article on how the CLR creates Managed Objects to see how things are different.
Managed C++ is a set of extensions to C++ that Microsoft introduced to allow the unmanaged and managed worlds to interoperate. There are others such as COM and reusing unmanaged DLLs (P/Invoke). Managed C++ allows you to have more precision in controlling the interoperation, aswell as allowing you to write .net programs in the C++ language.
There is good tutorial here on Managed C++. That tries to bridge the worlds.