Mixed Mode Programming is the absolute power of C ++/CLI

Source: Internet
Author: User

From this article in codeproject.

 

Mixed Mode Programming is the absolute power of C ++/CLI, and so is C ++/CLI the superior and mightiest of all programming languages. c ++/CLI is to C ++, as it is to C. you can do C Programming in C ++. in the same sense, you can do unmanaged C ++ programming in C ++/CLI without using any of the managed features, not even a Managed class. (I wocould imagine a reason to do that sort of a thing for the rest Of my life .) also, you can do pure managed programming without using any of the unmanaged practices. you can also do mixed mode programming, which means you can write an application that has both managed and unmanaged classes interacting with each other. so, a managed object can contain or interact with an unmanaged object, and vice versa. cocould you imagine the power of programming that you can un Leash with C ++/CLI? The simplest application of the above power is when you want to port your existing hi-fi image processing or math library written in C ++ to work on. NET platform. and, when you have not enough budget/time to rewrite it in C # (or VB. net, wocould you try that), you can recompile your existing code with C ++/CLI and write a (managed) wrapper so that they can be used by any. NET programming language. it does not take much effort to write a wrapper when you compare the effort of rewriting and testing it. following is a Managed class that interacts with an unmanaged object :-

ref class ManagedClass
{
private: UnmanagedClass *unmgdPtr;

public: ManagedClass(UnmanagedClass *unmgdClassPtr)
{
// similar to _ASSERTE(..); see nullptr usage.

System::Diagnostics::Debug::Assert(unmgdClassPtr != nullptr);
this->unmgdPtr = unmgdClassPtr;
}

public: // Some methods that use the unmgdPtr

};

class UnmanagedClass
{
public: UnmanagedClass()
{
}

public: void SomeUnmgdMethod()
{
}

// imagine a ton of other public methods


};

Likewise, an unmanaged class can bear a managed reference as its member and can invoke methods on it. but like a Managed class holding the pointer to unmanaged, it cannot directly have the reference; instead, it is the following way :-

// ref class Managed Class - Some managed class

class UnmanagedClass
{
private: gcroot<ManagedClass^> mgdRef;
public: UnmanagedClass(ManagedClass^ mgdClassRef)
{
Debug::Assert(mgdClassRef != nullptr);
this->mgdRef = mgdClassRef;
}

// Methods that use mgdRef and invoke methods: mgd->SomeMethod();


};

gcrootIs itself an unmanaged entity which has the ability to refer to managed entities. So, an instancegcroot<managed>Can be a statically or dynamically allocated member inside the unmanaged class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.