简体   繁体   中英

Keeping member variables into smart pointers and importance of explicitly defined destructor

I have a code that was written using old style c++, such as raw pointers like is shown below(code 1):

class Thing { 
    private:
        int data;
        Thing* one;
        Thing* second;
        Thing* previous;
    public:
        Thing():one(0), second(0), previous(0) {}
        // Here is my point of focus
        ~Thing() {
            delete one;
            delete second;
            delete previous;
        }
};

class Container {
    private:
        Thing* thing_ptr;
    public:
        Container() : thing_ptr(new Thing()) {}
        void make_empty {
        /*
            Some algorithm for manually destroying the whole structure.
        */
        }
        ~Container() {
            delete thing_ptr;
        }
};

My aim is to use smart pointers instead of raw pointers and do something like this (code 2):

class Thing { 
    private:
        int data;
        std::shared_ptr<Thing> one;
        std::shared_ptr<Thing> second;
        std::shared_ptr<Thing> previous;
    public:
        Thing() : one(nullptr), 
                  second(nullptr), 
                  previous(nullptr) {}

        // Here is my point of focus
        ~Thing() {
        // Do some stuff
        }
};

class Container {
    private:
        std::shared_ptr<Thing> thing_ptr;
    public:
        Container(): thing_ptr(nullptr) {}
        void make_empty {
        /*
            Some recursive algorithm for manually destroying the whole structure.
        */
        }
        /* Some other functions */
        ~Container() {
            // Do some stuff
        }
};

Case 1. How the compiler can delete the pointers that are holding in shared pointers if I will not provide the appropriate destructors? And will the procedure of deletion contain any destructor call from my classes?

Case 2. If there is no explicitly defined destructors and there are some other shared_ptr member variable that holds an object of class Field in class Thing like this:

class Field {...}

class Thing { 
    private:
        int data;
        std::shared_ptr<Field> field;
        std::shared_ptr<Thing> one;
        std::shared_ptr<Thing> second;
        std::shared_ptr<Thing> previous;
    public:
        Thing() : field(nullptr)
                  one(nullptr), 
                  second(nullptr), 
                  previous(nullptr) {}

        // Here is my point of focus
        ~Thing() {
            // Do some stuff
        }
};

Will the Field's destructor be invoked? If no then how compiler decides how to delete the stuff properly? Are there drawbacks?

The destructors of member variables are automatically invoked after the contents of your destructor. The constructors are invoked in the opposite order of construction.

Also, you should be using unique_ptr not shared_ptr. shared_ptr does reference counting which you almost certainly do not need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM