最佳答案
我基本上不理解 Clang 的 -Wweak-vtables
。以下是我到目前为止观察到的:
案例一: (触发警告)
class A {
public:
virtual ~A(){}
};
class B : public A {
public:
virtual ~B(){}
};
int main(){}
案例二: (不触发警告)
class A {
public:
virtual ~A(){}
};
int main(){}
案例三: (不触发警告)
class A {
public:
virtual ~A();
};
A::~A(){}
class B : public A {
public:
virtual ~B(){}
};
int main(){}
案例四: (触发器警告)
class A {
public:
virtual ~A(){}
virtual void fun(){}
};
class B : public A {
public:
virtual ~B(){}
};
int main(){}
案例5: (不触发警告)
class A {
public:
virtual ~A(){}
virtual void fun();
};
class B : public A {
public:
virtual ~B(){}
};
int main(){}
案例6: (不触发警告)
class A {
public:
virtual ~A(){}
virtual void fun(){}
};
class B : public A {};
int main(){}
案例七: (不触发警告)
class A {
public:
virtual ~A(){}
virtual void fun(){}
};
class B : public A {
public:
virtual void fun(){}
};
int main(){}
确切的警告是
warning: 'A' has no out-of-line virtual method definitions; its vtable
will be emitted in every translation unit [-Wweak-vtables]
所以很明显,如果我没有在类中声明一个非内联虚函数,它会导致一些 只有当我从它派生出来并且派生类有一个虚析构函数时才会出现这种问题。
问题: