Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overload a destructor?

Tags:

c++

How do I overload a destructor?

like image 346
Joshua Avatar asked Jun 12 '09 20:06

Joshua


People also ask

Can we overload destructor in C?

You can't. There is only one destructor per class in C++.

Can we overload destructor in Java?

It does not accept any parameter and cannot be overloaded.

What is a constructor What is a destructor can it be overloaded?

Source. Constructor and Destructor are the special member functions of the class which are created by the C++ compiler or can be defined by the user. Constructor is used to initialize the object of the class while destructor is called by the compiler when the object is destroyed.

Can we override destructor in C++?

After a destructor has been created as a pure virtual object(instance of a class), where the destructor body is provided. This is due to the fact that destructors will not be overridden in derived classes, but will instead be called in reverse order.


1 Answers

You can't. There is only one destructor per class in C++.

What you can do is make a private destructor and then have several public methods which call the destructor in new and interesting ways.

class Foo {
  ~Foo() { ... }
public:
  DestroyFoo(int) { ... };
  DestroyFoo(std::string) { ... }
};
like image 175
JaredPar Avatar answered Sep 21 '22 01:09

JaredPar