Destructors. Definition of the destructor. Public and private destructors. Examples of destructors using. Differences between constructors and destructors
Contents
- 1. What is the purpose of destructor in the class?
- 2. An example of using a public destructor
- 3. Can I declare the destructor in the ‘private’ section?
- 4. In what cases it is advisable to declare a private destructors?
- 5. What are the limitations when working with class objects, if a private destructor is declared in the class?
- 6. Can a destructor have parameters?
- 7. What are the main differences between the use of constructors and destructors in classes?
- 8. Video lesson. Class destructor
- Related topics
Search other websites:
1. What is the purpose of the destructor in the class?
The destructor, like the constructor, belongs to the special functions of the class. A destructor is a special method that is called when an object is deleted. Typically, the destructor is used to free memory dynamically allocated for the internal data of the class. There may be other cases of using the destructor.
A destructor is a function inverse to the constructor. The destructor name is the same as the class name, beginned by the character ‘~’.
For example, if the class has the name CMyClass, then the name of the destructor will be ~CMyClass().
2. An example of using a public destructor
Let the class, that defines an array of structures of type DATE, be given. Each structure in the array describes the date in the format: number, month, year. A several constructors, methods, and a destructor are declared in the class, which frees the memory for the array. General code of the module in which the structure and class are declared.
// structure that implements a date struct DATE { int day; int month; int year; }; // class defining an array of structures of type DATE class CDates { int n; DATE *A; // array of structures public: CDates(void); // default constructor ~CDates(void); // destructor // class methods void Set(int nn); // set a new value n, allocate memory for array A again DATE GetDATE(int index); // return the value of the structure with the index 'index' }; // default constructor CDates::CDates(void) { n = 0; } // destructor CDates::~CDates(void) { // freeing the memory allocated for an array of structures delete A; } // set a new value n void CDates::SetN(int nn) { // 1. Free the previous memory delete A; // 2. Set a new value n n = nn; // 3. Select memory for an array of pointers to a type of structure (DATE *) A = new DATE[n]; // fill the fields of each structure with arbitrary values for (int i=0; i<n; i++) { A[i].day = i; A[i].month = i%12 + 1; A[i].year = 1000*i; } } // return the value of the structure with the index DATE DATE CDates::GetDATE(int index) { return (DATE)A[index]; }
Demonstration of using this class in another method.
CDates CD; // declare an object of type CDates, the default constructor is called // set a new value n CD.Set(8); // test int t; DATE D; D = CD.GetDATE(5); // take a structure with index 5 t = D.day; // t = 5 t = D.year; // t = 5000 // after exiting the method, the CD object is destroyed, and, hence, the destructor ~CDates() is called // which frees memory for array A in the class
3. Can I declare the destructor in the ‘private’ section?
Yes. Such destructor is called a private destructor.
4. In what cases it is advisable to declare a private destructors?
The use of private destructors is advisable in those cases when the usual methods are forbidden to free memory for previously created objects.
5. What are the limitations when working with class objects, if a private destructor is declared in the class?
If a private destructor is declared in the class, then the following restrictions occur:
- it is impossible to create an automatic object of a class (an object of a class that is declared in some method);
- it is impossible to create a static object;
- it is impossible to create a global class object.
This is due to the fact that such objects can not be destroyed in the future.
For example. Let the class in which the private destructor is declared is given.
class CMyInt { private: int n; // number of elements in the array // destructor declared in the 'private' section ~CMyInt(void); public: CMyInt(void); // class methods void SetN(int nn) { n = nn; } int GetN(void) { return n; } };
If you try to create a class object in another program code
CMyInt MI;
the compiler will generate an error
'CMyInt::~MyInt': cannot access private member declared in class 'CMyInt'
However, you can declare a pointer to the CMyInt class. The following description will be correct
// private destructor CMyInt * pMI = new CMyInt; // You can declare a pointer to a class that contains a private destructor
6. Can a destructor have parameters?
A destructor can not have parameters.
7. What are the main differences between the use of constructors and destructors in classes?
When used in a class, the following main differences can be between the constructor and the destructor:
- destructors can not take any parameters, but constructors can take parameters;
- destructors can be virtual, constructors – can not;
- when declaring a class, you can declare only one destructor. However, you can declare any number of constructors. The main thing is that they differ signature.
8. Video lesson. Class destructor
Contents
- C++. Class constructor. Features of use constructors in the classes. The default constructor. Parameterized constructor. Examples of calsses that contain constructors
- C#. Classes. Class definition. Object of class. Constructor of the class. Destructor. “Garbage collection”. Keyword this.