mardi 22 décembre 2020

Am I allowed to define an empty function (body) in the virtual destructor in header, or will I have to define it in the source file?

C++11

    class Node {
    public:
        virtual NodeType kind() const = 0;
        virtual std::string str() const = 0;
        virtual ~Node() {}
    };

I decided to use a virtual destructor because in my derived classes of Node, vectors and unique_ptr are used, and if I don't have a virtual destructor, the memory of the vector won't be freed. Here was why I added destructor (related issue): valgrind shows memory leak in vector constructor

For my virtual destructor, may I define it as a {} in the header file, or will I have to define it like virtual ~Node(); in the header and Node::~Node() {} in the source (cpp) file?

I heard it was possible to do an empty function in constructor when using initializer list in the header file, so I wonder if it is possible in a destructor? Post related to that: Is it required to define the initialization list in a header file?

Aucun commentaire:

Enregistrer un commentaire