Destructor Overload
28 September, 2013 - 2 min read
Can I overload the destructor for my class?
No. Destructors never have parameters or return values.
And you're not supposed to call destructors explicitly, so you couldn't use parameters or return values anyway.
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) { ... }
};
Overloading means having several functions with the same name which take different arguments. Likeswap(int &a, int &b)
and swap(double &a, double &b)
. A destructor takes no arguments. Overloading it would not make sense.
Constructor
Constructors have the same name as the
class.
Constructors do not return a value.
The constructor, like all functions, can be
overloaded with each constructor having a
different set of parameters.
Destructor
Destructors have the same name as the class
but preceded with a ~.
Destructors do not return a value.
Destructors take no arguments and cannot be
overloaded.
Destructors are used for cleaning up object data
/ state
Allocated memory
Close files
Close network connections, etc.
Constructor Summary
Date d1(3, 10, 2002); // constructor called
Date d2, d5; // default constructor called
Date d3 (d2); // copy constructor called
Date d4 = d1; // copy constructor called
d5 = d2; // assignment operator called.
END