
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Private Destructor in C++
Here we will see what will be the case if the destructors are private in C++. Let us see some example codes to get the idea.
This code has private destructor, but it will not generate any error because no object is created.
Example
#include <iostream> using namespace std; class my_class { private: ~my_class(){ //private destructor } }; int main() { }
In this program, this will generate compilation error, as we are trying to create one object, but the compiler can notice that the destructor is not accessible. So it cannot be destroyed after completing the task.
Example
#include <iostream> using namespace std; class my_class { private: ~my_class() { //private destructor } }; int main() { my_class obj; }
Output
[Error] 'my_class::~my_class()' is private
Now if we create one pointer for that class, then it will not generate error because no actual object is created.
Example
#include <iostream> using namespace std; class my_class { private: ~my_class() { //private destructor } }; int main() { my_class *obj; }
If one object is created using the new operator, then also no error will generate. The compiler thinks that this is programmer’s responsibility to delete the object from memory.
Example
#include <iostream> using namespace std; class my_class { private: ~my_class() { //private destructor } }; int main() { my_class *obj = new my_class; }
Output
Now if we add delete statement to delete the object, it will generate error for the private destructor.
Example
#include <iostream> using namespace std; class my_class { private: ~my_class() { //private destructor } }; int main() { my_class *obj = new my_class; delete obj; }
Output
[Error] 'my_class::~my_class()' is private
- Related Articles
- Virtual Destructor in C++
- Pure virtual destructor in C++
- Order of Constructor/ Destructor Call in C++
- Private Variables in C#
- Private Methods in C#
- Why do we need a pure virtual destructor in C++?
- attribute((constructor)) and attribute((destructor)) syntaxes in C in tutorials point ?
- Private and final methods in C#
- Private and Protected Members in C++
- Difference Between Constructor and Destructor
- Private Constructors and Singleton Classes in C#
- Difference Between Private and Protected in C++
- What is the difference between a destructor and a free function in C++?
- How to initialize private static members in C++?
- How does the destructor method __del__() work in Python?
