I basically do not understand clang's -Wweak-vtables
. Here is what I observed so far:
Case one: (triggers the warning)
class A {
public:
virtual ~A(){}
};
class B : public A {
public:
virtual ~B(){}
};
int main(){}
Case two: (Does not trigger warning)
class A {
public:
virtual ~A(){}
};
int main(){}
Case three: (Does not trigger warning)
class A {
public:
virtual ~A();
};
A::~A(){}
class B : public A {
public:
virtual ~B(){}
};
int main(){}
Case four: (Triggers warning)
class A {
public:
virtual ~A(){}
virtual void fun(){}
};
class B : public A {
public:
virtual ~B(){}
};
int main(){}
Case five: (Does not trigger warning)
class A {
public:
virtual ~A(){}
virtual void fun();
};
class B : public A {
public:
virtual ~B(){}
};
int main(){}
Case six: (Does not trigger warning)
class A {
public:
virtual ~A(){}
virtual void fun(){}
};
class B : public A {};
int main(){}
Case seven: (Does not trigger warning)
class A {
public:
virtual ~A(){}
virtual void fun(){}
};
class B : public A {
public:
virtual void fun(){}
};
int main(){}
The exact warning is
warning: 'A' has no out-of-line virtual method definitions; its vtable
will be emitted in every translation unit [-Wweak-vtables]
So apparently, if I do not declare a non-inline virtual function in a class, it causes some
kind of problem if and only if I derive from it and the derived class has a virtual destructor.
Questions:
- Why is this a problem?
- Why does this get fixed by declaring a virtual function? (Warning speaks of
definitions)
- Why does the warning not occur when I do not derive from the class?
- Why does the warning not occur when the derived class does not have a virtual destructor?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…