The C++ specification says the default destructor deletes all non-static members. Nevertheless, I can't manage to achieve that.
I have this:
class N {
public:
~N() {
std::cout << "Destroying object of type N";
}
};
class M {
public:
M() {
n = new N;
}
// ~M() { //this should happen by default
// delete n;
// }
private:
N* n;
};
Then this should print the given message, but it doesn't:
M* m = new M();
delete m; //this should invoke the default destructor
The default destructor is destroying the pointer. If you want to delete the
N
withM
's default destructor, use a smart pointer. ChangeN * n;
toauto_ptr<N> n;
andn
will be destroyed.Edit: As pointed out in comments,
auto_ptr<>
is not the best smart pointer for all uses, but it looks like what's called for here. It specifically represents ownership: the N in an M is there for the duration of the M and no longer. Copying or assigning anauto_ptr<>
represents a change in ownership, which is usually not what you want. If you wanted to pass a pointer from M, you should pass aN *
gotten fromn.get()
.A more general solution would be
boost::shared_ptr<>
, which will be in the C++0x standard. That can be used pretty much wherever a raw pointer would be used. It's not the most efficient construct, and has problems with circular references, but it's generally a safe construct.Another edit: To answer the question in another comment, the standard behavior of the default destructor is to destroy all data members and base classes. However, deleting a raw pointer simply removes the pointer, not what it points to. After all, the implementation can't know if that's the only pointer, or the important one, or anything like that. The idea behind smart pointers is that deleting a smart pointer will at least lead to the deletion of what it points to, which is usually the desired behavior.
What makes you think the object
n
points to should be deleted by default? The default destructor destroys the pointer, not what it's pointing to.Edit: I'll see if I can make this a little more clear.
If you had a local pointer, and it went out of scope, would you expect the object it points to to be destroyed?
The
t
pointer is cleaned up, but theThing
it points to is not. This is a leak. Essentially the same thing is happening in your class.Try avoiding using pointers. They are last resort elements.
Then use it this way
This will display Destroying object of type N
Since you are using
new
to create instance, it won't delete by default.Imagine something like this:
You're on your own with pointers in C++. No one will automatically delete them for you.
The default destructor will indeed destroy all member objects. But the member object in this case is a pointer itself, not the thing it points to. This might have confused you.
However, if instead of a simple built-in pointer, you will use a smart pointer, the destruction of such a "pointer" (which is actually a class) might trigger the destruction of the object pointed to.
M destructor should have 'delete n'.