Managed and Unmanaged Data in a Managed Library

The Arithmetic class is a managed class; it was defined with the _gc keyword as follows :

 
 public __gc class Arithmetic { // . . . }; 

When you create instances of this class, they must be created on the managed heap:

 
 ManagedMathLibrary::Arithmetic* a = new ManagedMathLibrary::Arithmetic(); 

When you're finished with them, you don't clean them up with the delete operator. You don't do anything; the garbage collector will clean up the memory for you later.

The __gc keyword is an addition to the C++ language. You can write managed code that declares unmanaged classes. Just omit the __gc keyword, like this:

 
 class Simple { private:    int x; public:    Simple(int xx): x(xx){};    int getx() {return x;} }; 

When you're writing managed C++, you can use unmanaged classes such as Simple , creating instances either on the heap or the stack.

 
 Simple s(3); Console::WriteLine(__box(s.getx())); Simple* ps = new Simple(4); Console::WriteLine(__box(ps->getx())); 

However, you must remember to clean up any instances you create on the heap, or your program will suffer a memory leak. Just use the delete operator, like this:

 
 delete ps; 

Visibility

C++ programmers are used to controlling the visibility of member variables and functions with the public , private , and protected keywords. In a managed class, the public keyword can also be applied to a class.

The Arithmetic class was declared with the __gc keyword, but also with the public keyword. Without the public modifier, the class would not be available to managed code outside the assembly, such as the ManagedHarness console application and the CSUseLibrary C# console application. You can use the unmanaged class Simple from outside the assembly only if it is defined entirely in its .h file, and you include that file in the code that will use the class. This results in the class being recompiled into that assembly. If parts of the Simple code are in the .cpp file, you won't be able to use the class from another assembly.

When you design a managed class library, you will plan for one or more garbage-collected classesmanaged data in your managed code. You can choose whether to expose these classes to managed code outside the assembly with the public keyword. You may also write some unmanaged classes, none of which will be accessible outside the assembly.

Accessing Legacy Libraries Such as the STL

One example of unmanaged data that can be very useful in a C++ application is a template instance. The Standard Template Library gathers together numerous template classes and functions, such as collection classes (linked lists, queues, and so on) and common operations (sorting, finding, and the like). Using the STL from your C++ applications requires no special work, whether you're working in managed or unmanaged C++.

What's more, the C++ compiler in Visual C++ .NET 2003 is over 98% compliant with the ANSI C++ standard. This astonishing level of compatibility opens up a world of libraries to Visual C++ developers. A number of template-based libraries exist that have tremendous power and can save you days or weeks of programming effort, including

  • Boost (www.boost.org) is a community-supported outgrowth of the STL. The libraries within Boost handle collections, mathematics, and memory management.

  • Blitz (www.oonumerics.org/blitz) is a class library for scientific computing with an emphasis on high-speed code.

  • Loki (www.moderncppdesign.com) is a library of generic components that implement design patterns.

These libraries make intensive use of templates and of advanced template features. Parts of these libraries would not build on earlier versions of Visual C++. When you write managed code for the .NET runtime in Visual C++, these libraries are available to save time and effort.



Microsoft Visual C++. NET 2003 Kick Start
Microsoft Visual C++ .NET 2003 Kick Start
ISBN: 0672326000
EAN: 2147483647
Year: 2002
Pages: 141
Authors: Kate Gregory

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net