Should Destructors Be Private?

Whenever we want to control destruction of objects of a class, we make the destructor private. For dynamically created objects, it may happen that you pass a pointer to the object to a function and the function deletes the object. If the object is referred after the function call, the reference will become dangling.

Are destructors public?

Properties of Destructor:

Destructor function is automatically invoked when the objects are destroyed. It cannot be declared static or const. ... A destructor should be declared in the public section of the class. The programmer cannot access the address of destructor.

What are the rules for destructors?

Destructor rules
  • Name should begin with tilde sign(~) and must match class name.
  • There cannot be more than one destructor in a class.
  • Unlike constructors that can have parameters, destructors do not allow any parameter.
  • They do not have any return type, just like constructors.
James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.