C++ 在C++;11

C++ 在C++;11,c++,c++11,virtual-destructor,C++,C++11,Virtual Destructor,我有一个接口类,但我希望所有派生类都实现虚拟析构函数: // Interface.h class Interface { virtual ~Interface() = 0; }; Interface::~Interface() = default; 问题是,在这种情况下,我有一个链接器错误,因为重复的符号 我可以将定义放在.cpp文件中,但我想知道是否有更优雅的解决方案?您可以在之前添加内联。根据此语法,可以: decl-specifier-seq(optional) ~ class

我有一个接口类,但我希望所有派生类都实现虚拟析构函数:

// Interface.h
class Interface {
    virtual ~Interface() = 0;
};

Interface::~Interface() = default;
问题是,在这种情况下,我有一个链接器错误,因为重复的符号


我可以将定义放在
.cpp
文件中,但我想知道是否有更优雅的解决方案?

您可以在之前添加内联。根据此语法,可以:

decl-specifier-seq(optional) ~ class_name () = default;

decl-specifier-seq  -   friend, inline, virtual, or nothing (no return type) 

可能
内联接口::~Interface()=默认值?函数(析构函数)不能是抽象的,不能同时具有实现(默认值)。@AndrewKashpur,是的,它可以:为什么要使析构函数纯虚拟?它没有添加任何内容。所有类都必须实现(或默认)析构函数。@SanderDeDycker当类没有任何成员函数使其成为纯虚函数时,将其抽象是一个常见的技巧。