Зміст курсу
C++ OOP
C++ OOP
What is a Destructor of the Class
Syntax of Destructor
Even though they serve opposite purposes creating a destructor is very similar to creating a constructor. They share almost the same declaration principles. The general approach to creating one is:
![code example](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/7e5aa9aa-cc31-4221-8c3c-17e302f66dfd/code_pics/s2c7.png)
- Name: It has the same name as the class, but is preceded by a tilde (~).
- No Return Type: It does not have a return type, not even void.
- No Parameters: It cannot take any parameters.
- Automatic Invocation: It is called automatically by the compiler when the object goes out of scope or is explicitly deleted.
Automatic Invocation of Destructors
The automatic invocation of destructors refers to the automatic calling of the destructor. This ensures that resources held by the object are properly released, thus helping to prevent memory leaks and resource leaks.
scope.cpp
delete.cpp
termination.cpp
- When an object goes out of scope.
- When the delete operator is used to delete dynamically allocated objects.
- When the program terminates.
The Need of Destructors
The primary purpose of a destructor is to release resources acquired by the object during its lifetime. This includes closing file handles, deallocating memory (using new) or simmilar tasks.
ResourceHolder.cpp
FileHandler.cpp
Все було зрозуміло?
Зміст курсу
C++ OOP
C++ OOP
What is a Destructor of the Class
Syntax of Destructor
Even though they serve opposite purposes creating a destructor is very similar to creating a constructor. They share almost the same declaration principles. The general approach to creating one is:
![code example](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/7e5aa9aa-cc31-4221-8c3c-17e302f66dfd/code_pics/s2c7.png)
- Name: It has the same name as the class, but is preceded by a tilde (~).
- No Return Type: It does not have a return type, not even void.
- No Parameters: It cannot take any parameters.
- Automatic Invocation: It is called automatically by the compiler when the object goes out of scope or is explicitly deleted.
Automatic Invocation of Destructors
The automatic invocation of destructors refers to the automatic calling of the destructor. This ensures that resources held by the object are properly released, thus helping to prevent memory leaks and resource leaks.
scope.cpp
delete.cpp
termination.cpp
- When an object goes out of scope.
- When the delete operator is used to delete dynamically allocated objects.
- When the program terminates.
The Need of Destructors
The primary purpose of a destructor is to release resources acquired by the object during its lifetime. This includes closing file handles, deallocating memory (using new) or simmilar tasks.
ResourceHolder.cpp
FileHandler.cpp
Все було зрозуміло?