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...
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?
- 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.