Why do C++ objects have a default destructor?
When I don't declare a constructor for example, the compiler will provide me with a default constructor that will have no arguments and no definition (empty body), and thus, will take no action.
So, if I'm finished with an object for example, wouldn't the default destructor reallocate (free) memory used by the object? If it doesn't, why are we getting it?
And, maybe the same question applies to the default constructor. If it does nothing, why is it created for us by default?
It's wrong to say that a compiler-generated default constructor takes no action. It is equivalent to a user-defined constructor with an empty body and an empty initializer list, but that doesn't mean it takes no action. Here is what it does:
- It calls the base class'es default constructor.
- It initializes the vtable pointer, if the class is polymorphic.
- It calls the default constructors of all members that have them. If there is a member with some constructors, but without a default one, then it's a compile-time error.
And only if a class is not polymorphic, has no base class and has no members that require construction, then a compiler-generated default constructor does nothing. But even then a default constructor is sometimes necessary for the reasons explained in other answers.
The same goes for the destructor - it calls base class'es destructor and destructors of all members which have them, so it isn't true in general case that a compiler-generated destructor does nothing.
But memory allocation really has nothing to do with this. The memory is allocated before the constructor is called, and it is freed only after the last destructor has finished.
Because if you don't have any (publiclly-accessible) constructors or destructors, then an object of the class cannot be instantiated. Consider:
class A
{
private:
A() {}
~A() {}
};
A a; // Oh dear! Compilation error
If you don't explicitly declare any constructors or destructors, the compiler must provide one to allow creation of objects.
When using smart pointers, the default destructor (see Sergey's answer) can be critical to avoid memory leaks. Here an example:
#include <iostream>
#include <memory>
using namespace std;
class Foo {
public:
Foo(int n = 0): n(n) { cout << "Foo(" << n << ")" << endl; }
~Foo() { cout << "~Foo(" << n << ")" << endl; }
private:
int n;
};
// notes:
// * default destructor of Bar calls destructors of unique_ptr<Foo> foo
// and of unique_ptr<Foo[]> foo3, which, in turn, delete the Foo objects
// * foo2's Foo object leaks
class Bar {
public:
Bar(): foo(new Foo(1)), foo2(new Foo(2)), foo3(new Foo[2]) { }
private:
unique_ptr<Foo> foo;
Foo* foo2;
unique_ptr<Foo[]> foo3;
};
int main() {
Bar bar;
cout << "in main()" << endl;
}
Here the output, showing that a leak occurs only for foo2
:
Foo(1)
Foo(2)
Foo(0)
Foo(0)
in main()
~Foo(0)
~Foo(0)
~Foo(1)
Default destructor won't do anything (just like a default constructor).
You'll need to define one yourself, if your destructor actually needs to do something (eg: freeing some resources).
Note that usually you should follow the rule of three: if your program needs to do something in its destructor (eg: freeing resources) you should also provide a copy constructor and an assignment operator; C++ also provides default versions of them (which, again, won't do anything).
Default constructor/destructor/assignment operator/copy constructor are useful when you're handling simple classes where you don't need to do anything. A special case are POD: they (prior to C++0x) even cannot have explicit constructors or destructor.