Can we overload destructor of a class in C++?

Answer: No, we cannot overload a destructor of a class in C++ programming. Only one empty destructor per class should be there. It must have a void parameter list. Destructor in C++ neither takes any parameters nor does it return anything.

So, multiple destructor with different signatures are not possible in a class. Hence, overloading is also not possible.

In below class, if we try to overload destructor for the class A, the compiler will flash an error. For example, when we define destructor as ~A(int a) {} , compiler will throw error stating “ a destructor must have a void parameter list”.

//Destructor can't be overlodded sample
class A{
public:
	A(){
		cout<<"constructor...";
	}
	//Destructor
	~A(){
		cout<<"Destructor...";
	}

	//compiler error: : a destructor must
        // have a 'void' parameter list

	~A(int a){
		cout<<"Destructor overloaded...";
	}
};

int main(){
	A o;
	return 0;
}

NOTES:

1)Constructor overloading in C++ is possible. You can read another related topic advantage of constructor overloading in C++.

2)Destructor in C++ cannot be overridden too. Don’t get confused

with virtual destructor in C++ .

If we try to override desturctor, then compiler will flash an error, example below.

//Destructor can't be overridden sample
class D:public A{
public:
	D(){
		cout<<"Derived constructor...";
	}
	~D(){
		cout<<"Derived Destructor...";
	}

	//If try to override parent class destructor, compiler
	//throws error 'D::~A' : destructor tag mismatch	
	//'D::~D(void)' : member function already defined or declared
	~A(){
		cout<<"Overrided Destructor...";
	} 

};

Related Posts