Question: Do I have to explicitly destroy objects in order

Question

Do I have to explicitly destroy objects in order

Answers 4
Added at 2016-12-27 18:12
Tags
Question

I know that when the implicit constructors are provided for you your class members are initialised from left to right and top to bottom. In other words, in the order that they're declared. Then when the class object goes out of scope all members are destructed in reverse order. But if I have to destroy members myself do I have to do it in the exact order that they are listed? For example:

struct Dog {};

struct Animal
{
    Dog* dog1 = new Dog;
    Dog* dog2 = new Dog;
    ~Animal() 
    {
        delete dog2;     // First delete dog2
        delete dog1;     // Then delete dog1
    // Or do I?
    }
};

I think the destructor that's provided is an empty one. So when I hear that the class will call the destructors of its members when it goes out of scope, it doesn't do that in its own destructor, but after it, with code that the compiler generates separately? So for example:

struct Animal
{
     Dog dog1;
     // ~Animal(){}implicitly defined destructor here. dog1 isn't destroyed here
    // But is destroyed here, or after, or something along those lines with separate code that the compiler generates?

};

Thanks.

Answers to

Do I have to explicitly destroy objects in order

nr: #1 dodano: 2016-12-27 18:12

Remember that there's a distinction between deleting the object pointed at by a pointer and destroying the pointer itself. C++ will automatically destroy the pointers themselves when the containing object's lifetime ends, and it will do so in the reverse of the order in which they were constructed. You're free to delete those pointers in any order you'd like, depending on how the object is set up.

As a note, if you change from using raw pointers to unique_ptrs, then this would be completely handled for you and there would be no need to even have a destructor at all!

nr: #2 dodano: 2016-12-27 18:12

But if I have to destroy members myself do I have to do it in the exact order that they are listed?

No, do it in whatever order you like.

But I prefer to match the "automatic" order, to keep from being surprised.

I think the destructor that's provided is an empty one. So when I hear that the class will call the destructors of its members when it goes out of scope, it doesn't do that in its own destructor, but after it, with code that the compiler generates separately?

Basically, yes.

Execution of the destructor body is one part of destruction. Another part is destruction of all the members and bases.

nr: #3 dodano: 2016-12-27 18:12

The order that you destroy your objects in your destructor generally does not matter, unless there is some reason that it should based on the objects owned resources needs.

For instance, if you are deleting a owned thread object, you probably want to make sure the thread has exited before deleting items that thread might use. The shared_ptr and other smart pointers are helpful in those situations. They also help with reducing the need for destructors in general.

nr: #4 dodano: 2016-12-27 20:12

As answered by Lightness Races in Orbit, You can follow any order to destroy the members of class. But the default destructor (if not called explicitly) always follow a particular order to destroy the members which is in reverse order than that of the members created.

As destructor is also a function, you can add statements in that function to be executed at the destruction time of object. Then, destructor will execute those statements and then, it will proceed to delete the members of class in reverse order.

For example:

struct Dog {};

struct Animal
{
    Dog* dog1 = new Dog;
    Dog* dog2 = new Dog;
    int var;

    ~Animal() 
    {
        delete dog1;     // First delete dog1
    }   //delete var and then delete dog2
};

Destructor will execute it's body and then, it will start deleting it's remaining members in reverse order.

Source Show
◀ Wstecz