January 27, 2021
Hot Topics:

C++/CLI: Managed Development with C++

  • By Nick Wienholt
  • Send Email »
  • More Articles »

Both lines of code will result in a new instance of the MyManagedClass class being allocated on the managed heap, but the c1 variable is logically a stack-based reference to a MyManagedClass object whereas c2 uses the object handle syntax to reference a heap-based object. The key difference is that the dispose method of c1 (~MyManagedClass()) will be called as soon as c1 goes out of scope, in exactly the same way as it would for a native type. The object that the c2 variable references will behave the same way as a native object, and ~MyManagedClass will not be called until the delete method is called on c2. For experienced C++ developers, having C++/CLI behave exactly as C++ should behave in the critical area of resource management is a huge improvement over other managed language alternatives.

One of the other nice features of C++/CLI is automatic generation of backing store for trivial properties. Properties are a first class .NET concept, and allow access to variables through accessor methods using the same syntax as public member variable access. The key benefit of properties is that they support natural and consistent syntax while allowing the implementation of property accessor methods to change without necessitating a re-compilation of all code the accesses the property. Property get and set methods that merely forward access to an underlying private or protected member variable are tedious to implement, and for this case, C++/CLI can do the tedious work under the hood, allowing a property to be written the same as a member variable with the context-sensitive property keyword before the declaration:

ref class MyManagedClass{
   property int Count;


For the experienced C++ developer who wants to produce managed classes, C++/CLI is an excellent language. C++/CLI is designed from the ground up to feel like C++, and although some of the syntax, such as the object handle, will initially appear strange to the eye, the behaviour of C++/CLI code is designed to feel like native C++; this avoids leading the developer unknowingly into coding pitfalls.

C++/CLI contains a number of innovative managed language features like automatic object cleanup and stream-lined property implementation. Building on the lessons learnt in both Managed Extensions for C++ and C#, C++/CLI is a modern and clean extension of C++ into the world of the .NET Framework.

About the Author

Nick Wienholt is an independent Windows and .NET consultant based in Sydney. He is the author of Maximizing .NET Performance and co-author of A Programmer's Introduction to C# 2.0 from Apress, and specialises in system-level software architecture and development, with a particular focus of performance, security, interoperability, and debugging.

Nick is a keen and active participant in the .NET community. He is the co-founder of the Sydney Deep .NET User group and writes technical articles for Australian Developer Journal, ZDNet, Pinnacle Publishing, Developer.COM, MSDN Magazine (Australia and New Zealand Edition), and the Microsoft Developer Network. An archive of Nick's SDNUG presentations, articles, and .NET blog is available at www.dotnetperformance.com.

In recognition of his work in the .NET area, Nick was awarded the Microsoft Most Valued Professional Award from 2002 through 2008.

Page 2 of 2

This article was originally published on October 6, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date