0
votes

I have seen here and here that a good rule of thumb is to use virtual destructors for every class that is intended as a base class. I have a pure abstract base class (only contains pure virtual functions and no data members; intended to be used as an interface).

class A {
    public:
        virtual void foo() = 0;
};

Is it possible to add a virtual destructor to this class without creating an implementation file (this class is defined in a header file included in several .cpp files) just for an empty destructor while also avoiding clang and g++ warnings such as -Wweak-vtables? I cannot put the empty definition in the header file with the pure abstract class because I will get multiple definitions of the destructor.

1
" will get multiple definitions of the destructor." - no, you won't, if it is inline in the class - have you tried?user2100815
virtual ~A() = default;user4581301
Or virtual ~A() {}Drew Dormann
@Neil Butterworth This triggers the -Wweak-vtables from clangAdam Sperry
Hm, I think that duplicate is a different question.Drew Dormann

1 Answers

2
votes

Whereas, clang generates warning for:

class A {
public:
    virtual ~A() {}
    virtual void foo() = 0;
};

using = default doesn't trigger it.

class A {
public:
    virtual ~A() = default;
    virtual void foo() = 0;
};

Even if both are valid.

Demo