When do we need to define destructors? [duplicate]

The rule of Three and The Rule of Zero

The good ol’ way of handling resources was with the Rule of Three (now Rule of Five due to move semantic), but recently another rule is taking over: the Rule of Zero.

The idea, but you should really read the article, is that resource management should be left to other specific classes.

On this regard the standard library provides a nice set of tools like: std::vector, std::string, std::unique_ptr and std::shared_ptr, effectively removing the need for custom destructors, move/copy constructors, move/copy assignment and default constructors.

How to apply it to your code

In your code you have a lot of different resources, and this makes for a great example.

The string

If you notice brandname is effectively a “dynamic string”, the standard library not only saves you from C-style string, but automatically manages the memory of the string with std::string.

The dynamically allocated B

The second resource appears to be a dynamically allocated B. If you are dynamically allocating for other reasons other than “I want an optional member” you should definitely use std::unique_ptr that will take care of the resource (deallocating when appropriate) automatically. On the other hand, if you want it to be an optional member you can use std::optional instead.

The collection of Bs

The last resource is just an array of Bs. That is easily managed with an std::vector. The standard library allows you to choose from a variety of different containers for your different needs; Just to mention some of them: std::deque, std::list and std::array.

Conclusion

To add all the suggestions up, you would end up with:

class A {
private:
    std::string brandname;
    std::unique_ptr<B> b;
    std::vector<B> vec;
public:
    virtual void something(){} = 0;
};

Which is both safe and readable.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)