问题
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 (body), and thus, will take no action.
If I now don't declare a destructor
, the compiler will provide me with a default destructor
with no defintion (body), and thus, I think 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 doesn nothing, why is it created for us by default?
Thanks.
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
The default constructor and destructors are just a commodity in case you do not need anything special done with your class you do not need to write an empty version manually. This is common to other OO languages, for example in Java you do not need to provide a constructor if zero initialization of the members suffices. At the same time it is a requirement for backwards compatibility with C. If you have a struct
in C, it will not have constructor or destructor (C does not have those concepts), to be able to handle that code in C++ that has to be valid code.
Another option would have been declaring in the language that a class could have no constructor or destructor, but then the whole language spec would have to deal with the fact that some types might have constructors and destructors while others don't, and that will make the language more complex and harder to specify. While having that implicitly defined versions does not change the behavior and eases the specification.
At this level it is like the term overrider being applied to the method in the base class. In the base class, it does not override anything, there is nothing to override! And yet the language explicitly states that a virtual non-pure method declared in a base is an overrider. This enables the spec to simply say that the final overrider will be called when the method is called through a pointer or reference without having to add an extre *or the base method implementation if no overrider exists for that particular method in that particular hierarchy.
回答5:
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)
回答6:
The short answer is that in C++ every object needs a constructor and a destructor, even if they don't do anything. So the compiler creating them for you in the background satisfies this requirement.
A longer answer is that the constructors are responsible for initialization of members of the class. The default constructor does default initialization of all of the members. (That doesn't mean anything for POD types, but other classes get their default constructors called.)
回答7:
A default destructor would have no way of knowing what memory your class "owns" to be able to free it.
As to the default constructor part, I will quote the Wikipedia article on this one...
In C++, default constructors are significant because they are automatically invoked in certain circumstances:
- When an object value is declared with no argument list, e.g. MyClass x;; or allocated dynamically with no argument list, e.g. new MyClass; the default constructor is used to initialize the object
- When an array of objects is declared, e.g. MyClass x[10];; or allocated dynamically, e.g. new MyClass [10]; the default constructor is used to initialize all the elements
- When a derived class constructor does not explicitly call the base class constructor in its initializer list, the default constructor for the base class is called
- When a class constructor does not explicitly call the constructor of one of its object-valued fields in its initializer list, the default constructor for the field's class is called
- In the standard library, certain containers "fill in" values using the default constructor when the value is not given explicitly, e.g. vector(10); initializes the vector with 10 elements, which are filled with the default-constructed value of our type.
In the above circumstances, it is an error if the class does not have a default constructor. The compiler will implicitly define a default constructor
if no constructors are explicitly defined for a class. This implicitly-declared default constructor is equivalent to a default constructor defined with a blank body. (Note: if some constructors are defined, but they are all non-default, the compiler will not implicitly define a default constructor. This means that a default constructor may not exist for a class.)
来源:https://stackoverflow.com/questions/4837223/c-default-destructor