I am beginner for Visual C++ and currently just learning the concepts of it.
I came to know that there are 2 classes: Managed class & Value class.
- Value classes are like normal C++ classes, whose objects can be created which will hold the data.
- Managed classes are memory managed by garbage collector.
Questions:
- Objects of the managed classes can't be created only handles can be created. Why is this?
- Please give me brief idea about an instance? Is it just an object creation or something else?
It is not that you only create handles for managed classes, the instances (or objects) of the managed classes are created on the managed heap and you are given a handle to access that instance.
The full answer is a wiki entry in it's own right, but I'll try give you an idea of what the issues here are;
- Managed vs. Value classes are C++/CLI (.Net) type classes. The reference documentation relating to C# and .Net is still valid here and will probably answer some of the more subtle questions here.
- A "reference" you speak of is a .Net reference. Some people compare this to a pointer (a smart pointer) and that may help understand some of the code, but it is not a pointer.
- Just because it is C++, doesn't mean the usual .Net rules don't apply. The mixed mode is there to allow code to cross the native/.Net boundary, but on either side the native/.Net, respectively, the rules still apply. Speaking from experience, try to keep this "contact" area small and specific helps in dealing with the subtleties and nuances when you try to have one foot in each camp.
- Essentially, a managed class must have all it's members managed as well, since the garbage collection of this data is non-deterministic.
- In a similar manner, "native data" in a mixed mode application cannot contain managed classes (or references).
gcroot
and raw pointers are generally used to mix the two. RAII classes help to manage these elements, but can be specific to your project, so general solutions don't always help.