Managed and unmanaged classes in Visual C++. Overview. Examples of implementing managed and unmanaged classes
Contents
- 1. What kinds of classes exist in Visual C ++? What are the features of using different types of classes?
- 2. An example of using a class that implements an array of n numbers
- Related topics
Search other websites:
1. What kinds of classes exist in Visual C ++? What are the features of using different types of classes?
In the Visual C ++ system, two types of classes are used:
- unmanaged-classes. This is a classic way of describing and using the class. Such classes are used in conjunction with managed-pointers ‘*’. For such pointers, memory is allocated using the new operator;
- managed-classes. These are classes that are intended to be used in the Common Language Runtime (CLR). Such classes are stored in memory, which is used and controlled by the CLR. Objects of these classes are placed in the dynamic memory. If you want to declare a pointer to a managed class, then the symbol ‘^’ is used for this. The memory for this pointer is allocated by the gcnew utility.
Working with pointers to a class is described in detail in the following topics:
- Pointers. Part 3. Unmanaged pointers and arrays. Pointer to structure. Pointer to class;
- Pointers. Part 6. Composite managed and native data types. Managed pointers (^) in CLR. Memory allocation. The ref and value qualifiers. Managed pointers to structures and classes.
2. An example of using a class that implements an array of n numbers
Below are examples of the implementation of classes of two types:
- unmanaged-class (native-class);
- managed-class.
2.1. Unmanaged class
2.1.1. Working with class methods using a class object
In the example, the unmanaged class MyArrayUnmanaged is implemented, which is an array of double numbers of dimension n.
In the class such internal variables are declared:
- n – the current number of array elements (array length);
- A – array of numbers of ‘double’ type.
The following constructors are defined in the class:
- default constructor MyArrayUnmanaged(). This constructor zeroes the value of n;
- a constructor that initializes the value of n and array A. The constructor takes one parameter.
The following methods are implemented in the class:
- GetN() – returns the current value of n;
- SetN() – sets a new value of n, array A is automatically set to zero. The memory for the array is overridden;
- GetAi() – returns the value of the specified array element;
- SetAi() – sets a new value for the specified array element;
- GetArrayPtr() – a demonstration method that returns an unmanaged pointer (*) to an array.
class MyArrayUnmanaged { // declaration of an array in an unmanaged class int n; double *A; public: MyArrayUnmanaged(void); // default constructor ~MyArrayUnmanaged(void); // destructor MyArrayUnmanaged(int nn) // constructor with 1 parameter { n = nn; // allocating memory for an array A = new double[n]; // array filling with zero values for (int i=0; i<n; i++) A[i] = i; } // class methods // take the value of n int GetN(void) { return n; } // set a new value of n void Set(int nn) { // free up memory for the previous array if (n>0) delete A; n = nn; A = new double[n]; // allocate memory for a new array // zeroing a new array for (int i=0; i<n; i++) A[i] = 0; } // return the value of the i-th element of the array double GetAi(int i) { return (double)A[i]; } // set a new value in the i-th element of the array void SetAi(int i, double x) { A[i] = x; } // get an unmanaged pointer to an array double * GetArrayPtr(void) { return (double *)A; } }; // implementation of class constructors // default constructor MyArrayUnmanaged::MyArrayUnmanaged(void) { n = 0; } // destructor MyArrayUnmanaged::~MyArrayUnmanaged(void) { if (n>0) delete A; }
Demonstration of using a class in another program code
// Demonstration of work with the class MyArrayUnmanaged MyArrayUnmanaged MA; // object of the class, the default constructor is called int d; double x; d = MA.Get(); // d = 0 MyArrayUnmanaged MA2(15); // a constructor with 1 parameter is called d = MA2.Get(); // d = 15 MyArrayUnmanaged MA3 = 25; // a constructor with 1 parameter is called d = MA3.GetN(); // d = 25 x = MA3.GetAi(5); // x = 5.0 MA3.SetAi(5, -3.45); // change value in cell 5 of MA3 array x = MA3.GetAi(5); // x = -3.45 // using of method GetArrayPtr() double * B; // unmanaged-pointer to double B = MA3.GetArrayPtr(); // B points to array A of MA3 object x = B[3]; // x = 3.0 x = B[5]; // x = -3.45 // using of method SetN() MA3.SetN(5); d = MA3.GetN(); // d = 5 x = MA3.GetAi(3); // x = 0
2.1.2. Working with class methods using a pointer (*) per class
It is demonstrated how to work with an unmanaged pointer to a class.
MyArrayUnmanaged * p; // declare a pointer to a class p = new MyArrayUnmanaged(10); // a constructor with 1 parameter is called int d; double x; d = pA->GetN(); // d = 10 x = pA->GetAi(3); // x = 3
2.2. An example of the implementation of managed class
In the example below, the managed class CMyArray is declared, which implements an array of n real (double) numbers. The dimension of the array changes dynamically.
The class implements the following internal (private) variables:
- n – the current number of array elements;
- A – array of numbers.
Class contains two constructors:
- CMyArray() without parameters. This is the default constructor. In this constructor, the value of n is set to zero;
- CMyArray() with one parameter, which is equal to the size of the array (n). Here, the value of the elements of the array is set equal to the index number of the array.
Class methods are as follows:
- GetN() – get the current length of the array n;
- SetN() – set new value of n and zero values to the array elements A;
- GetAi() – get the current value of the array element with index i;
- GetArrayA() – get a pointer to an array;
- SetAi() – set a new value to the array element with index i.
The class implementation is placed in several modules (files):
- file ‘MyArray.h’ – contains the code of the class CMyArray;
- file ‘MyArray.cpp’ – contains the implementation of the methods of the class. In this case, this module contains the implementation of class constructors. The implementation of other methods is described in the class itself.
The text of MyArray.h module is as follows:
#pragma once // managed-class CMyArray ref class CMyArray { // Declaring an array of size n in managed class int n; array <double^> ^A; public: // default constructor CMyArray(void); // constructor with 1 parameter CMyArray(int nn); // Class methods // take the value of n int GetN(void) { return n; } // set a new value of n void SetN(int nn) { if (n>0) delete A; n = nn; // new value A = gcnew array <double^>(n); for (int i=0; i<n; i++) A[i] = gcnew double; for (int i=0; i<n; i++) A[i] = (double)0; } // take an array element with index i (i = 1..n-1) double GetAi(int i) { return (double)A[i]; } // take the pointer to array array <double^> ^ GetArrayA(void) { return A; } // set a new value to an array element with index i (i=1..n-1) void SetAi(int i, double nx) { A[i] = nx; } };
The text of MyArray.cpp is as follows:
#include "StdAfx.h" #include "MyArray.h" // default constructor CMyArray::CMyArray(void) { n = 0; } // constructor with 1 parameter CMyArray::CMyArray(int nn) { n = nn; // allocating memory for an array (override) A = gcnew array <double ^>(n); // allocating memory for each cell in the array for (int i=0; i<n; i++) A[i] = gcnew double; // filling with values of the array elements for (int i=0; i<n; i++) A[i] = (double)i; }
2.2.1. Working with class methods using a class object
To use the CMyArray class in other modules (files), you need to connect the MyArray.h module
#include "MyArray.h"
The code below shows how to use the methods of the CMyArray class.
// Demonstration of work with managed class object // 1. Calling the constructor without parameters (it will be called automatically) CMyArray MA; // object of class CMyArray, the constructor without parameters CMyArray() is called int d; d = MA.GetN(); // d = 0 // 2. Calling a constructor with 1 parameter when creating an object, method 1 CMyArray MA2(15); // the CMyArray(15) constructor is called int d2 = MA2.GetN(); // d2 = 15 double x; x = MA2.GetAi(5); // x = 5.0 // 3. Calling a constructor with 1 parameter when creating an object, method 2 CMyArray MA3 = 20; // the CMyArray(20) constructor is called int d3; d3 = MA3.GetN(); // d3 = 20 x = MA3.GetAi(18); // x = 18.0 // 4. Demonstrate the returning of an array from the GetArrayA class method CMyArray MA4 = 30; // 30 elements in the array array <double^> ^B; B = MA4.GetArrayA(); // Take an entire array A from the class x = (double)B[22]; // x = 22.0 // 5. Method SetAi CMyArray MA5 = 10; // declare an array of 10 elements MA5.SetAi(3, 2.85); // call the SetAi method x = MA5.GetAi(3); // x = 2.85 // 6. Method SetN MA5.SetN(30); d = MA5.GetN(); // d = 30 x = MA5.GetAi(3); // x = 0
Let’s explain some code fragments. When declaring a class object
CMyArray MA;
the default constructor is CMyArray() is called.
If you need to specify the dimension of the array when it is declared, then for the CMyArray class, you can write
CMyArray MA2(10);
or
CMyArray MA2 = 10;
where the number 10 indicates the dimension of the array.
Methods for processing class data are described in the section public. Methods are called in the standard way using the symbol ‘ . ‘.
2.2.2. Working with class methods using a class pointer (^)
You can also describe a pointer to the managed-class. The following code snippet demonstrates the use of a pointer to the CMyArray class.
// managed-pointer to class CMyArray p; p = gcnew CMyArray(10); // allocating memory for a class (array with 10 elements), the constructor is called double x; x = p->GetAi(5); // x = 5
In the above code in the line
pA = gcnew CMyArray(10);
The CMyArray() constructor is called with one parameter. The constructor allocates memory for the array and filling the array elements with values.