[Mono-devel-list] C++/CLI and .NET 2.0

Jonathan Pryor jonpryor at vt.edu
Thu Jul 1 23:14:54 EDT 2004


On Thu, 2004-07-01 at 21:21, Michael Torrie wrote:
> If C++ programs are compiled to pure CLR apps, does that not mean that
> the guaranteed destruction semantics of C++ will not apply?

Remember the C++/CLI mantra: It Just Works.

>From a C#-programmers view of C++/CLI, destructors are mapped to an
IDisposable implementation, which are philosophically invoked from a
finally block.  This is also bi-directional: classes containing an
IDisposable implementation are seen as having destructors from within
C++/CLI.  This can make for elegant code.

Consequently, the C++/CLI code:

	String^ ReadFirstLineFromFile (String^ path)
	{
		StreamReader reader (path);
		return reader.ReadLine ();
	}

is identical to the C# code:

	string ReadFirstLineFromFile (string path)
	{
		using (StreamReader reader = new StreamReader (path)) {
			return reader.ReadLine ();
		}
	}

This was taken from the MSDN Magazine article I referenced:

	http://msdn.microsoft.com/msdnmag/issues/04/05/VisualC2005/default.aspx

C++/CLI also adds new syntax to override the finalizer: !<classname>:

	public ref class Test {
	public:
		// Constructor
		Test ();

		// Destructor: mapped to IDisposable.Dispose()
		~Test ();

		// Finalizer: overrides Object.Finalize()
		!Test ();
	};

The short of it is that existing C++ code should Just Work, unchanged,
within the CLI environment.

> If this is
> the case then there are many C++ apps that would require some work if
> they depend on this behavior (some of my C++ code depends on this).  I
> believe this is one big reason why C++ on .NET will never be a huge
> success.

I would suggest reading up on C++/CLI more before passing judgements
like this. :-)

You might find Stan Lippman's blog to be helpful, as he's the primary
author behind C++/CLI:

	http://blogs.msdn.com/slippman/

> Would compiling C++ via whirl allow this somewhat critical c++
> feature to work on .NET?

It would have to do the same thing (or similar) as C++/CLI.

 - Jon





More information about the Mono-devel-list mailing list