Building a Managed Class Library

The library in this chapter serves the same purpose as the unmanaged library in the previous chapter: It has one class, named Arithmetic , which has four methods : Add , Subtract , Divide , and Multiply . These methods each take two System::Double parameters and return a System::Double answer.

To create the sample library, start by using Visual Studio .NET 2003 to create a Class Library (.NET) project called ManagedMathLibrary . This creates an implementation file, ManagedMathLibrary.cpp, and a header file, ManagedMathLibrary.h. The compiler doesn't care whether you put the entire implementation into the header file or put the declarations into the header file and the method bodies into the implementation file.

When you work in unmanaged code, you move as much of the code as you can into an implementation file to keep the header small, because all the code that uses the class must include the header file. However, when you work with managed code, the header file is not included: The reference to the assembly provides the compiler with the information it needs. So in managed code, it doesn't matter where you put your method bodies. For simplicity, the sample code in this chapter is in the header file.

The class that is created in the ManagedMathLibrary project is called Class1 change it to Arithmetic in ManagedMathLibrary.h. There's no code to change in ManagedMathLibrary.cpp. Then you add the methods and their bodies right in the header file. The class ends up looking like this:

 
 // ManagedMathLibrary.h #pragma once using namespace System; namespace ManagedMathLibrary {    public __gc class Arithmetic    {    public:       System::Double Add(System::Double num1, System::Double num2)       {          return num1 + num2;       }       System::Double Subtract (System::Double num1, System::Double num2)       {          return num1 - num2;       }       System::Double Divide(System::Double num1, System::Double num2)       {          return num1 / num2;       }       System::Double Multiply(System::Double num1, System::Double num2)       {          return num1 * num2;       }    }; } 

There is very little difference between this code and the code you would write for the same library in unmanaged code. The differences are as follows :

  • The keyword public on the class declaration ensures that in the assembly built for this project, the Arithmetic class will be available for use by classes in other assemblies.

  • The keyword __gc on the class declaration makes this class garbage-collected ; its memory will be managed by the runtime.

  • The method parameters and return types are System::Double .

System::Double is a managed value type, equivalent to the double type in unmanaged C++. Unlike the fundamental type double , System::Double is a structure, with member functions. It inherits from System::Object and its memory is managed by the runtime. As a value type, you can create instances on the stack; you don't need to use pointer semantics to work with your instances. As you can see in the code for these member functions, operator overloads for the basic arithmetic operations have already been defined for this type. You can think of it as being just like double , but under the hood it's very different.



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