简体   繁体   English

在C ++ 11中默认纯虚拟析构函数的正确放置

[英]Proper placement for the default pure virtual destructor in C++11

I have an interface class but I want all derived classes to implement the virtual destructor: 我有一个接口类,但我希望所有派生类都实现虚拟析构函数:

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

Interface::~Interface() = default;

The problem is that in this case I have a linker error because of duplicated symbols. 问题是在这种情况下,由于符号重复,我有一个链接器错误。

I can place the definition in .cpp file but I'd like to know if there is more elegant solution? 我可以将定义放在.cpp文件中,但我想知道是否还有更优雅的解决方案?

You can add inline before. 您可以在之前添加内联。 According to http://en.cppreference.com/w/cpp/language/destructor this syntax is OK: 根据http://en.cppreference.com/w/cpp/language/destructor,此语法可以:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM